[Proto-Scripty] Re: Enumerable sort descending

2011-05-09 Thread T.J. Crowder
On May 9, 3:40 am, kstubs kst...@gmail.com wrote:
 How do you sort descending?  Also, how do you distinguish between Alpha,
 Alpha-numeric, and Numeric sorts?

 Thanks, loving the methods like sort() and uniq() off enumerable?

 Karl..

`Enumerable` doesn't have a `sort` function. `Array` does, it's part
of JavaScript; `Enumerable` has `sortBy` which is similar. (One big
difference is that `Array#sort` processes the array in-place;
`Enumerable#sortBy` *returns* a new array of the elements in the
desired order, leaving the original `Enumerable` unchanged.)

In both cases (`Enumerable#sortBy` and `Array#sort`), you can pass in
a function that will get called to compare two elements during the
sorting process. The function should return less than zero if the
first argument is less than the second (e.g., should be before it in
your sort order), zero if they're the same, or greater than zero if
the first is greater than the second (should be after it). That
means you're entirely in control of how the elements are sorted. Doing
a reverse sort, or dealing with numeric vs. alpha, is entirely down to
how you write the function that you pass into the `sort` / `sortBy`
function.

MDC has some examples of `Array#sort` that may be useful:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

HTH,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

-- 
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: Enumerable sort descending

2011-05-09 Thread kstubs
Thanks for the tips.  Since my posting I discovered sortBy().  So I'm close, 
but don't see how/where to pass in the 2nd argument to compare.  I have:

list = list.sortBy(function(s) {
if(field.field.startsWith('EventScore') || field.field == 
'AAScore')
return Number(Object.values(s)[index]);
else
return Object.values(s)[index];
});


Reason for the if/else is pretty straightforward; this fields beginning with 
EventScore or AAScore are numeric.  Anyhow, how does the 2nd argument work? 
 The above works perfectly, but strictly ascending.

Karl..

-- 
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: Enumerable sort descending

2011-05-09 Thread Julien Lenne
Hello,

Have you tried to return with a minus operand to reverse the order ?

Regards,
Julien.

On May 9, 9:46 am, kstubs kst...@gmail.com wrote:
 Thanks for the tips.  Since my posting I discovered sortBy().  So I'm close,
 but don't see how/where to pass in the 2nd argument to compare.  I have:

 list = list.sortBy(function(s) {
                 if(field.field.startsWith('EventScore') || field.field ==
 'AAScore')
                     return Number(Object.values(s)[index]);
                 else                
                     return Object.values(s)[index];
             });

 Reason for the if/else is pretty straightforward; this fields beginning with
 EventScore or AAScore are numeric.  Anyhow, how does the 2nd argument work?
  The above works perfectly, but strictly ascending.

 Karl..

-- 
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: Enumerable sort descending

2011-05-09 Thread T.J. Crowder
Hi,

Sorry, I gave you a bum steer there. `sortBy` works completely
differently from `sort`. Apparently, you only get given one element
(and its index, as a second parameter the documentation doesn't
mention, perhaps it's not explicitly supported) and you're meant to
return some piece of information that will later be used to sort the
result. (`sortBy` builds up an array of the criteria you provide, and
then does a `sort` on it.) You have no control over the order, it will
always be ascending. So if you were sorting by something numeric,
you could play games with the sign of the values you return or
something, but you're out of luck if you're sorting based on strings
or the like. Instead, just create your own temporary array (perhaps
via `map`) and sort it.

Sorry for the bum steer.
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com


On May 9, 8:46 am, kstubs kst...@gmail.com wrote:
 Thanks for the tips.  Since my posting I discovered sortBy().  So I'm close,
 but don't see how/where to pass in the 2nd argument to compare.  I have:

 list = list.sortBy(function(s) {
                 if(field.field.startsWith('EventScore') || field.field ==
 'AAScore')
                     return Number(Object.values(s)[index]);
                 else                
                     return Object.values(s)[index];
             });

 Reason for the if/else is pretty straightforward; this fields beginning with
 EventScore or AAScore are numeric.  Anyhow, how does the 2nd argument work?
  The above works perfectly, but strictly ascending.

 Karl..

-- 
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: Enumerable sort descending

2011-05-09 Thread Julien Lenne
Hi,

So to answer your questions that were asked at first :
- To sort descending an Array :
  - You sort it with the JS native function sort()
  - You use sortBy and return -your_array.indexOf(current_element)

- To sort alphanumeric fields :
  - It's already done by the sort function, but if you want to change
this, add/substract max values of the field in the sortBy function to
place it where you want.

All of this is theorical, it heavely depends on your data et what you
want to achieve with it. Maybe a practical example of what you wish to
accomplish could help ;)

Regards,
Julien.

On May 9, 10:04 am, T.J. Crowder t...@crowdersoftware.com wrote:
 Hi,

 Sorry, I gave you a bum steer there. `sortBy` works completely
 differently from `sort`. Apparently, you only get given one element
 (and its index, as a second parameter the documentation doesn't
 mention, perhaps it's not explicitly supported) and you're meant to
 return some piece of information that will later be used to sort the
 result. (`sortBy` builds up an array of the criteria you provide, and
 then does a `sort` on it.) You have no control over the order, it will
 always be ascending. So if you were sorting by something numeric,
 you could play games with the sign of the values you return or
 something, but you're out of luck if you're sorting based on strings
 or the like. Instead, just create your own temporary array (perhaps
 via `map`) and sort it.

 Sorry for the bum steer.
 --
 T.J. Crowder
 Independent Software Engineer
 tj / crowder software / com
 www / crowder software / com

 On May 9, 8:46 am, kstubs kst...@gmail.com wrote:







  Thanks for the tips.  Since my posting I discovered sortBy().  So I'm close,
  but don't see how/where to pass in the 2nd argument to compare.  I have:

  list = list.sortBy(function(s) {
                  if(field.field.startsWith('EventScore') || field.field ==
  'AAScore')
                      return Number(Object.values(s)[index]);
                  else                
                      return Object.values(s)[index];
              });

  Reason for the if/else is pretty straightforward; this fields beginning with
  EventScore or AAScore are numeric.  Anyhow, how does the 2nd argument work?
   The above works perfectly, but strictly ascending.

  Karl..

-- 
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: Enumerable sort descending

2011-05-09 Thread kstubs
OK TJ, I understand what to do, but is there a way to iterate backwards 
through the enumeration?  I guess I can get the size, and iterate over it 
with a decremental index value.

-- 
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: Enumerable sort descending

2011-05-09 Thread T.J. Crowder
Hi,

On May 9, 9:27 am, kstubs kst...@gmail.com wrote:
 OK TJ, I understand what to do, but is there a way to iterate backwards
 through the enumeration?  I guess I can get the size, and iterate over it
 with a decremental index value.

I'd use `Enumerable#toArray` and then either reverse it or loop
through it backward. (`Array` has a `reverse` function.)

-- T.J. :-)

-- 
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: Enumerable sort descending

2011-05-09 Thread kstubs
It does!  OK, so I had this:
var copylist = list;
list = new Array();
for(i = copylist.size(); i0; i--) {
list.push(copylist[i - 1]);
}

No I have this:
list = list.reverse();


Works!
Thanks,
Karl..

-- 
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: Enumerable sort descending

2011-05-09 Thread kstubs
Hey, what stops me from this:

list = list.sortBy(function(s) {
if(field.field.startsWith('EventScore') || field.field == 
'AAScore')
return Number(Object.values(s)[index]);
else
return Object.values(s)[index];
}).*revers()*;

-- 
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: Enumerable sort descending

2011-05-09 Thread T.J. Crowder
Hi,

 Hey, what stops me from this:

Nothing at all (since `Array#sort` operates on the array in place, but
does return the array reference as a return value as well). Well,
nothing other than the missing e on `reverse`. ;-)

Note that if you do that, `list` will be an `Array`, regardless of
what it was at the outset. If it's *already* an array, I'd ditch
`sortBy` and use `sort`.

Out of curiousity, what's in `list`? All those calls to
`Object.values` look like a lot of overhead...

-- T.J.

On May 9, 9:54 am, kstubs kst...@gmail.com wrote:
 Hey, what stops me from this:

 list = list.sortBy(function(s) {
                 if(field.field.startsWith('EventScore') || field.field ==
 'AAScore')
                     return Number(Object.values(s)[index]);
                 else                
                     return Object.values(s)[index];
             }).*revers()*;

-- 
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: Enumerable sort descending

2011-05-09 Thread kstubs
Fruit of my labors:
http://beta.meetscoresonline.com/results/11827

Now the results grid should populate on initial load of the page, so that is 
a bug (works locally, of course).  To get it to populate, you just need to 
drop down a navigation selection.

Currently working well on FF.

TJ., the list contains a pretty hefty object with fields.  Ultimately there 
is an Ajax request, the result is stored client side, then by means of 
navigation and sorts the list is widdled down to the results you see.  The 
reason for the Object.values is because I don't know the best way to go from 
TH click (which causes a sort) back to the original list and filter on 
that field.  There's a couple of lookups happening here.  Also, the results 
grid displays either 4 scores across plus AA score or 6 scores across plus 
AA, with different headings, so that isn't straight forward either.

Karl..

-- 
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.