alphanumeric sort

Eric Moritz eric at themoritzfamily.com
Wed May 11 15:59:24 EDT 2011


Your compare function will need to return a -1, 0 or 1 for less than,
the same or greater than.  Luckily javascript allows you
to strings lexicographically so the function is a nice two liner:

function(a, b) {
   if(a == b) return 0;
   return a > b ? 1 : -1;
}

Application:

var reduceSort = function (v, args) {
 return v.sort (function(a, b) {
   if(a['key'] == b['key']) return 0;
   return a['key'] > b['key'] ? 1 : -1;
 });
};

Eric.

On Wed, May 11, 2011 at 3:54 PM, Claus Guttesen <kometen at gmail.com> wrote:
> Hi.
>
> I have a function that performs a numeric sort (ascending) as
> described at http://siculars.posterous.com/using-riaks-mapreduce-for-sorting.
> But how to perform an alphanumeric sort?
>
> I'm using the code found in the link above:
>
> var reduceSort = function (v, args) {
>  return v.sort (function(a, b) { return a['key'] - b['key'] });
> };
>
> I'm writing a small app using express, riakjs and riak to keep track
> of crosscountry-races for kids, and would like to sort by name like:
>
> var reduceSort = function (v, args) {
>  return v.sort (function(a, b) { return a['fname'] - b['fname'] });
> };
>
> Hopefully I'm not asking dumb questions. :-)
>
> --
> regards
> Claus
>
> When lenity and cruelty play for a kingdom,
> the gentler gamester is the soonest winner.
>
> Shakespeare
>
> twitter.com/kometen
>
> _______________________________________________
> riak-users mailing list
> riak-users at lists.basho.com
> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
>




More information about the riak-users mailing list