#459 ✓invalid
Denk

sortBy - doesn't sort integers - code inside

Reported by Denk | November 21st, 2008 @ 02:57 PM

Hi

I'm creating a table sorter and it all works, except sorting a column that has integers in it's cells.

The reason why it doesn't sort them is because the integers are being compared to each other as string.

That's why I added a piece of code to the sortBy function for my website:


sortBy: function(iterator, context) {
    iterator = iterator.bind(context);
    return this.map(function(value, index) {
      return {value: value, criteria: iterator(value, index)};
    }).sort(function(left, right) {
      var a = left.criteria, b = right.criteria;

	  if(!isNaN(a) && !isNaN(b)){
		a = parseInt(a);
		b = parseInt(b);
	  }

      return a < b ? -1 : a > b ? 1 : 0;
    }).pluck('value');
  },

So I basically just added the if function to convert a and b (if they are both integers) to integers, that way they'll be compared correctly against each other

Kind regards Yannick

Comments and changes to this ticket

  • Denk

    Denk November 21st, 2008 @ 02:58 PM

    I seem to have misused the @@@ signs - sorry :)

  • Juriy Zaytsev
  • Juriy Zaytsev

    Juriy Zaytsev November 21st, 2008 @ 04:44 PM

    I'm not sure I'm following.

    So I basically just added the if function to convert a and b (if they are both integers) to integers, that way they'll be compared correctly against each other

    Why are you converting "integers" to "integers"?

  • Denk

    Denk November 21st, 2008 @ 06:45 PM

    1st of all: thx

    2nd of all: I'm not converting integers to integers. I'm converting strings to integers.

    The idea behind the tablesorter: You click on the

    element and then it'll sort the underlying 's based on their text (function: collectTextNodes()).

    So basically, I'm sorting STRINGS, but some are intended to be INTEGERS, that's why I have to convert the strings to integers

    Did this make more sense, if not, please ask for further explanation

    Kind regards, Yannick

  • Denk

    Denk November 21st, 2008 @ 06:46 PM

    omg... :p

    What a I wrote: 'You click on the TH-element and then it'll sort the underlying TD's based on their text .....

  • John-David Dalton
  • Denk

    Denk November 22nd, 2008 @ 11:58 AM

    Well

    Cuz my table also has strings in it

    Example table: USERNAME - NUMBER OF POSTS

    That's why I prefer not to 'parseInt()' them ^^

  • Samuel Lebeau

    Samuel Lebeau November 22nd, 2008 @ 02:07 PM

    This is not a Enumerable#sortBy issue.

    According to ECMAScript 262 specification, "<" relational operator, like all relational operators compare objects by values.

    Therefore the following just works :

    
    var array  = [{string: "4"}, {string: "-2"}, {string: "1"}],
        sorted = array.sortBy(function(o) { return o.string });
    
      sorted.pluck('string');
      // -> ["-2", "1", "4"];
    
  • Denk

    Denk November 22nd, 2008 @ 03:10 PM

    Try sorting 1, 4, 33, 2 (if they are STRINGS)

    It'll return 1, 2, 33, 4

    It should return: 1, 2, 4, 33

  • Samuel Lebeau

    Samuel Lebeau November 22nd, 2008 @ 03:44 PM

    You're absolutely right, sorry.

    Anyway, this still looks like it's an application-specific issue. There's nothing wrong with Enumerable#sortBybehavior which doesn't have to assume anything about what it's sorting.

    Couldn't you just distinguish the case where you know (from application semantics) a column contains integers, so you could map parseInt before sorting ?

  • Denk

    Denk November 22nd, 2008 @ 04:29 PM

    I'm not really sure if I can.

    The code is at work atm - but I basically just followed a book (about prototype and scriptaculous).

    I'll try it monday and I'll get back to you guys ;)

    Thanks so far lads, much appreciated :)

  • Tobie Langel

    Tobie Langel November 22nd, 2008 @ 05:05 PM

    • State changed from “new” to “invalid”

    This is clearly an application specific issue. Enum#sortBy's behaviour is correct. Closing as invalid.

  • John-David Dalton
  • Denk

    Denk November 24th, 2008 @ 10:07 AM

    So, here is the code that I've used

    
    var rows = this.body.childElements();
    	// rijen sorteren op basis van de text in de rij (row) in de kolom die overeenkomt met sortIndex
    	rows = rows.sortBy(function(row){
    		return row.childElements()[this.sortIndex].collectTextNodes();
    		}.bind(this));
    

    Any idea how can I can make it check if the text is an integer or a string or even a date?

    Kind regards Yannick

  • Denk

    Denk November 24th, 2008 @ 10:43 AM

    nm, i made this:

    
    var rows = this.body.childElements();
    		// rijen sorteren op basis van de text in de rij (row) in de kolom die overeenkomt met sortIndex
    		rows = rows.sortBy(function(row){
    			var value = row.childElements()[this.sortIndex].collectTextNodes();
    			if(!isNaN(value)){
    				return parseInt(row.childElements()[this.sortIndex].collectTextNodes());
    			}
    			return row.childElements()[this.sortIndex].collectTextNodes();
    		}.bind(this));
    

    That seems to do the trick. I'm sorry for bothering you guys and i'd like to thank you for your answers.

    (i do hope my solution is good ^^)

  • Tobie Langel

    Tobie Langel November 24th, 2008 @ 11:36 AM

    You should definitely seek assistance from the mailing list for this.

  • Denk

    Denk November 24th, 2008 @ 02:59 PM

    Yeah

    I'm sorry for creating a ticket. I didn't know I could fix this myself like this as I'm fairly new to actually exploring prototype (have written my own class and know how to use scriptaculous, but prototype is a lot bigger ^^)

    Will defo sign up for a few things regarding prototype :)

    and again sorry for wasting your time :)

    Kind regards, Yannick

  • kimbaudi

    kimbaudi August 27th, 2009 @ 05:59 AM

    • Tag cleared.

    Hi, sortBy actually does sort integers.

  • kimbaudi

    kimbaudi August 27th, 2009 @ 06:00 AM

    Hi, sortBy actually does sort integers.

    Try this using FireBug:

    var arr = [2, 5, 4, 8, 9, 1, 3, 10, 7, 6];
    console.log(arr.sortBy( function(e) { return e.length; } ));

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Pages