[Proto-Scripty] Re: How do sort a hash.
On Mar 2, 11:23 am, Richard Quadling wrote: > 2009/3/2 kangax : > > > > > > > On Mar 2, 9:58 am, Richard Quadling wrote: > >> Hi. > > >> This might be one of the those odd situations where there is no > >> answer, but I have a hash coming from PHP via JSONP to JS/Prototype. > > >> In IE and FF, the ordering is maintained. > > >> In Chrome the order is not maintained. > > >> The key part is numeric and is NOT in order. The value is sorted > >> alphabetically. > > >> I suppose I could reverse the key/value pairing and work that way, but > >> as the dox say, order is not guaranteed. > > >> Alternatively, as the data is going to be used for a tag, can > >> I sort the s by value? > > > Look into `sortBy`: > > > $H({ a: 20, b: 1, c: 10 }).sortBy(function(pair){ > > return pair.value; > > // or `return pair.key` (to sort by key) > > }); > > > [...] > > > -- > > kangax > > Argh!!! Thank you Kangax. > > I think this would be a great case for cross linking in the documentation. > > Hash can be thought of as an associative array, binding unique keys to > values (which are not necessarily unique), though it can not guarantee > consistent order its elements when iterating, "but sortBy() can > alleviate this issue." I actually find plain JS object sufficient most of the time for "hashtable" purposes. There are edge cases of course, but the performance gains of using plain objects (vs. `Hash` instances) can not be underestimated. > > Is there a way I can commit changes to the documentation? Sure. Create a ticket at Lighthouse and mark it as a documentation related enhancement. -- kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: How do sort a hash.
2009/3/2 kangax : > > On Mar 2, 9:58 am, Richard Quadling wrote: >> Hi. >> >> This might be one of the those odd situations where there is no >> answer, but I have a hash coming from PHP via JSONP to JS/Prototype. >> >> In IE and FF, the ordering is maintained. >> >> In Chrome the order is not maintained. >> >> The key part is numeric and is NOT in order. The value is sorted >> alphabetically. >> >> I suppose I could reverse the key/value pairing and work that way, but >> as the dox say, order is not guaranteed. >> >> Alternatively, as the data is going to be used for a tag, can >> I sort the s by value? > > Look into `sortBy`: > > $H({ a: 20, b: 1, c: 10 }).sortBy(function(pair){ > return pair.value; > // or `return pair.key` (to sort by key) > }); > > [...] > > -- > kangax > > > Argh!!! Thank you Kangax. I think this would be a great case for cross linking in the documentation. Hash can be thought of as an associative array, binding unique keys to values (which are not necessarily unique), though it can not guarantee consistent order its elements when iterating, "but sortBy() can alleviate this issue." Is there a way I can commit changes to the documentation? -- - Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: How do sort a hash.
On Mar 2, 9:58 am, Richard Quadling wrote: > Hi. > > This might be one of the those odd situations where there is no > answer, but I have a hash coming from PHP via JSONP to JS/Prototype. > > In IE and FF, the ordering is maintained. > > In Chrome the order is not maintained. > > The key part is numeric and is NOT in order. The value is sorted > alphabetically. > > I suppose I could reverse the key/value pairing and work that way, but > as the dox say, order is not guaranteed. > > Alternatively, as the data is going to be used for a tag, can > I sort the s by value? Look into `sortBy`: $H({ a: 20, b: 1, c: 10 }).sortBy(function(pair){ return pair.value; // or `return pair.key` (to sort by key) }); [...] -- kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: How do sort a hash.
2009/3/2 Richard Quadling : > Hi. > > This might be one of the those odd situations where there is no > answer, but I have a hash coming from PHP via JSONP to JS/Prototype. > > In IE and FF, the ordering is maintained. > > In Chrome the order is not maintained. > > The key part is numeric and is NOT in order. The value is sorted > alphabetically. > > I suppose I could reverse the key/value pairing and work that way, but > as the dox say, order is not guaranteed. > > Alternatively, as the data is going to be used for a tag, can > I sort the s by value? > > Regards, > > Richard > > > -- > - > Richard Quadling > Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 > "Standing on the shoulders of some very clever giants!" > Hey! I'm cooking today on gas today! Rather than ... $H(a_Values).each(...) I use ... $A($H(a_Values).each(function(h_Value) { return [h_Value.key, h_Value.value]; })).sort(function(a, b) { return a[1] < b[1] ? -1 : (a[1] > b[1] ? 1 : 0); }).each(...) Which looks really really messy, but works for me in Chrome, IE and FF. -- - Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---