Sunteți pe pagina 1din 3

05/ 11/ 2012

Sorting on the basis of Hash value while not overlooking the Keys

I have an array like : 'as,'o' 'as,'ras,'cem. cr' fr, rc' cem' sra' Now, what i do is that that i sort each word/string in this array and put that sorted string as a Value in a Hash while the original String is the Key. So now i have KY -VLE ES AUS cr -ar as cs fr-fr o o rc -ar as cs cem -aer ras cms sra -aer cem cms Code for this is sre_od =Hs.e() otdwrs ahnw0 wrsec {|od sre_od[od=odcassr {|,b acscpb }ji od.ah wr| otdwrswr]wr.hr.ot a | .aem() .on } After this i sort Hash on the basis of value by sre_od =sre_od.ot|1a|a[]=a[] otdwrs otdwrssr{a,2 11<>21} and acheive sra -aer cem cms cem -aer ras cms rc -ar as cs cr -ar as cs fr-fr o o Instead of this i am looking for the Hash to be arranged like cr -ar as cs rc -ar as cs cem -aer ras cms sra -aer cem cms fr-fr o o So, in the above list the values are in sorted position as in the list i am getting now but it also considers the Key value so thats why first is car and then all other keys with the same value and the cream and then for
ruby

asked Oct 10 at 1:35 LivingThing 512 2 9 69% accept rate

12.1k followers

rss

Ruby is an open-source dynamic object-oriented interpreted language created by Yukihiro Matsumoto (Matz) in 1993. faq info top users

'acemrs' is < 'acrs', why should those two follow the first two in your desired result? dbenhur Oct 10 at 5:43

st ackover f l w com / quest i ns/ / sor t i g- on- t he- basi - of - hash- val e- w l - not - over l oki g- t he- keys o . o n s u hi e o n

1/ 3

05/ 11/ 2012

Was this post useful to you?

Yes

No

3 Answers
It sounds like you want to sort on value major order with the keys breaking the tie. Try it like this: sre_od =sre_od.ot|k,1,k,2|vm =v<>2 vm!0?vm :k<>2} otdwrs otdwrssr{(1v)(2v) cp 1=v; cp= cp 1=k #= ["ras,"cms] [sra" "cms] [cr" "cs] [rc" "cs] [fr,"o"] > [cem" aer", "cem, aer", "as, ar", "as, ar", "o" fr]

answered Oct 10 at 5:44 dbenhur 3,463 1 9 Your answer makes sense, but you could have written it less obfuscated... Josh Voigts Oct 10 at 14:06 @JoshVoigts there is nothing obfuscated in my snippet. This is ordinary ruby used in its ordinary way. #sort, the yielding of hash members as key,value pairs, the use of the comparison operator <=>, and a ternary to chain to a secondary evaluation are quite prosaic and used as one would expect. dbenhur Oct 11 at 4:05

feedback

Hs[ ah ary ra .a{w [,wcassr_y&dwcs)ji] mp|| w .hr.otb(:onae.on} .otb{k v [,k} sr_y|, | v ] ]


answered Oct 10 at 7:38 sawa 12.6k 3 12 31 feedback

Please try this.. arr=['cars', 'for', 'racs', 'creams', 'scream'] sw=Hash.new mm=arr.each{|word|sw[word]=word.split(//).sort{|a,b|a.casecmp(b)}.join} ss=sw.sort_by{|key,value|[value[-1]]}.reverse # [["cars", "acrs"], ["racs", "acrs"], ["scream", "acemrs"], ["creams", "acemrs"], ["for", "for"]]
answered Oct 11 at 5:27 vijikumar 152 7 feedback

Not the answer you're looking for? Browse other questions tagged ruby or ask your own question.

st ackover f l w com / quest i ns/ / sor t i g- on- t he- basi - of - hash- val e- w l - not - over l oki g- t he- keys o . o n s u hi e o n

2/ 3

05/ 11/ 2012

st ackover f l w com / quest i ns/ / sor t i g- on- t he- basi - of - hash- val e- w l - not - over l oki g- t he- keys o . o n s u hi e o n

3/ 3

S-ar putea să vă placă și