If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

This seems to work. But this is sorting an ARRAY that is in a jQuery slickgrid - and I'm not so comfortable that "storing" the original sequence works when clicking different columns - looks ok - but I'm just not fully comfortable with this.

Has anyone ever run into this issue???

I need an R&D meeting with a big whiteboard a half-a-dozen other coders to flesh this out properly...

Why can't JS SORT be STABLE!!!

*** Read the sticky in the DB forum about how to get your question answered quickly!! ***

Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

Re: Javascript Array Sort method is "random" (NOT STABLE) within sort

I don't think I really understand the problem you're trying to solve. Can you elaborate on what you're trying to sort, how you were trying to do it (using vanilla sort()), what you got as a result and why it wasn't what you expected?

Re: Javascript Array Sort method is "random" (NOT STABLE) within sort

Specifying one of the FIELDS in the OBJECT list as the SORT column (this is a data feed to a SlickGrid).

Click the column of the slickgrid and it runs the sort. Click again and it sorts descending - by doing a .Reverse()

Sort again and it runs ascending - but now when you look at the results any place where there was DUPLICATE data in the sort column - and you look at anothner column (one that has the person's names, for example) it reversed the order of names.

Click again twice and the names are back to A-Z within the dups.

I consider that a NOT STABLE sort - EXCEL does not do that, for example.

And the web is alive with posts about non-STABLE JS sorts - as the spec for JS does not "require" stable sort - QUICKSORT is not stable - fix I read was to do a MERGESORT - yuk...

So I slept on it last night and decided I would put my own "tie-breaker" column in the objects within the array - so that when the COMPARER function hits a DUPLICATE pair it uses that "sequential" number to break the tie.

It doesn't get exactly the STABILITY that EXCEL shows - but at least it's consistent (EXCEL keeps a NAME column always "ASC" regardless of sorting DESC on a subsequent column).

Look at the image - EXCEL first at top - original unsorted list - then sort on COLUMN A (retains order of column B within A) then sort on COLUMN A DESC (retains ASC ORDER of column B within A) - then sort again on COLUMN A ASC (still retains ASC ORDER of column B within A).

Bottom is the BROKEN SORT prior to my fix - sort on the CAT column (asc first) - keeps EMPLOYEE name A-Z. Click CAT again - DESC sort - employees are reversed (as expected - I am calling .Reverse()). Click again - ASC CAT column - now names are Z-A! Click again - DESC CAT - but names are A - Z. Click again ASC CAT - now names are back to A - Z. That's really odd - since each click has exactly the same "presentation array".

You would have expected one result - then another - then back to the first result - but it's not happening that way - it's being NOT STABLE.

Customer expects SORT to be stable and not RANDOM.

My fix of adding a SEQUENTIAL # field to the objects in the array - and using that as the tie-breaker for dups - is at least making it so that each time the CAT column is sorted ASC the EMPLOYEE NAME column appears the same.

Attached Images

*** Read the sticky in the DB forum about how to get your question answered quickly!! ***

Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".