0

In this environment, I only have access to intrinsic Javascript functions, so cannot load external libraries.

When trying to sort by 3 keys, inner, middle, and outer, only my last sort seems to be preserved.

function Claim(claimNumber, lastName, claimStatus, record)
{
    this.claimNumber = claimNumber;
    this.lastName = lastName;
    this.claimStatus = claimStatus;
    this.record = record;
}
function sortLastName(a, b) { 
    var o1 = a["lastName"].toUpperCase(); 
    var o2 = b["lastName"].toUpperCase(); 
    if (o1 < o2) return -1; 
    if (o1 > o2) return 1; 
    return 0; 
} 
function sortClaimNumber(a, b) { 
    var o1 = a["claimNumber"].toUpperCase(); 
    var o2 = b["claimNumber"].toUpperCase(); 
    if (o1 < o2) return -1; 
    if (o1 > o2) return 1; 
    return 0; 
} 
function sortClaimStatus(a, b) { 
    var o1 = ("00" + a["claimStatus"].toUpperCase()).substr(-2); 
    var o2 = ("00" + b["claimStatus"].toUpperCase()).substr(-2); 
    if (o1 < o2) return 1; 
    if (o1 > o2) return -1; 
    return 0; 
} 
var claimListArray = buildClaimList(record);
claimListArray.sort(sortClaimStatus);
claimListArray.sort(sortClaimNumber);
claimListArray.sort(sortLastName);

The output should look like (lastname asc, claimnumber asc, claimstatus desc):

AARDVARK   111222A    15
AARDVARK   111222A    6
AARDVARK   111222A    1
AARDVARK   222555C    8
AARDVARK   222555C    4
BANKS      123132Z    78

but instead looks like:

AARDVARK   111222A    15
AARDVARK   222555C    4
AARDVARK   111222A    1
AARDVARK   222555C    8
AARDVARK   111222A    6
BANKS      123132Z    78

That is to say, only the lastName sort is preserved, as if the first two sorts did not happen. Is there something about arrays and sorting that I'm missing that ignores previous sorts?

Is there a better approach?

Robert Kerr
  • 1,291
  • 2
  • 17
  • 34

4 Answers4

2

try like this

function comp(a, b){
    var ret = sortLastName(a, b);
    if(ret != 0){
        return ret;
    }           
    ret = sortClaimNumber(a, b);
    if(ret != 0){
        return ret;
    }

    return sortClaimStatus(a, b);
}
claimListArray.sort(comp);

EDIT (by Malvolio):

Sorry to be editing your post but this solution, although technically correct, is bad Javascript. Write it like this:

claimListArray.sort(function(a, b) {
    return sortLastName(a, b)
        || sortClaimNumber(a, b)
        || sortClaimStatus(a, b);
});
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
Hacker Wins
  • 1,289
  • 9
  • 20
1

You need one single sorting method, otherwise you will re-sort the entire array:

function sortAll(a, b) { 
    var o1 = ("00" + a["claimStatus"].toUpperCase()).substr(-2); 
    var o2 = ("00" + b["claimStatus"].toUpperCase()).substr(-2); 
    if (o1 < o2) return -1; 
    if (o1 > o2) return 1; 
    //If they are equal, compare with claimNumber
    o1 = a["claimNumber"].toUpperCase(); 
    o2 = b["claimNumber"].toUpperCase(); 
    if (o1 < o2) return -1; 
    if (o1 > o2) return 1; 
    //If they are equal, compare with lastName
    o1 = a["lastName"].toUpperCase(); 
    o2 = b["lastName"].toUpperCase(); 
    if (o1 < o2) return 1; 
    if (o1 > o2) return -1; 
    return 0; 
}
Dennis
  • 32,200
  • 11
  • 64
  • 79
  • I had this approach originally without the complete result of the 3-layered sort. That's why I thought I had better try making 3 complete passes, each on a different key, starting on the outside and moving in. – Robert Kerr Jul 21 '12 at 15:20
  • There is no difference between the accepted answer and this one except the precedence of keys. – Dennis Jul 21 '12 at 15:37
  • You are correct. I revisited yours and after reordering the keys and testing, it is also fully functional. Thanks again. – Robert Kerr Jul 21 '12 at 15:47
0

Of course it happens that way because you are changing Claims element positions in the list 3 times. Each removes previously set positions. To achieve the order you want you cant just sort this array but rather create a view(maybe multidimensional array with keys for each Claim as one row) from Claims keys(which position can be changed across Claims elements).

Grzegorz Kaczan
  • 21,186
  • 3
  • 19
  • 17
0

A sort that respects the existing order of equivalent items in the input is called stable. As you've noticed, stability is an important feature in a sort. This question discusses the stability of the built-in sort in different browsers.

Community
  • 1
  • 1
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
  • Interesting, but I'm not using a browser. The environment is encapsulated Rhino, and an old old edition at that. – Robert Kerr Jul 21 '12 at 15:26
  • Check the docs for your version of Rhino: if the entry for `sort` doesn't guarantee stability, use a library sort or combine the comparators as HackerWins suggests. – Michael Lorton Jul 21 '12 at 19:28