SheppardPL
SheppardPL

Reputation: 85

Sorting comparator for specific case with upper and lowercase letters

I have a merge sort script like this:

function mergeSort(arr){
   var len = arr.length;
   if(len <2)
      return arr;
   var mid = Math.floor(len/2),
       left = arr.slice(0,mid),
       right =arr.slice(mid);
   //send left and right to the mergeSort to broke it down into pieces
   //then merge those
   return merge(mergeSort(left),mergeSort(right));
}


function merge(left, right){
  var result = [],
      lLen = left.length,
      rLen = right.length,
      l = 0,
      r = 0;
  while(l < lLen && r < rLen){
     if(left[l] < right[r]){
       result.push(left[l++]);
     }
     else{
       result.push(right[r++]);
    }
  }  
  //remaining part needs to be addred to the result
  return result.concat(left.slice(l)).concat(right.slice(r));
}

console.log(mergeSort(['a', 'B', 'b', 'A', 'c']))

the output is:

["A", "B", "a", "b", "c"]

I want to have order like

["A", "a", "B", "b", "c"]

toLowerCase() method will not work because it can return something like this:

["A", "a", "b", "B", "c"]

Regards

Upvotes: 0

Views: 278

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386654

Basically you could compare first only lower case characters and if equal, then reverse the result of String#localeCompare.

var array = ['a', 'B', 'b', 'A', 'c'];

array.sort(function (a, b) {
    var aa = a.toLowerCase(),
        bb = b.toLowerCase();

    return aa.localeCompare(bb) || b.localeCompare(a);
});

console.log(array);

With your code

function compare(a, b) {
    var aa = a.toLowerCase(),
        bb = b.toLowerCase();

    return aa.localeCompare(bb) || b.localeCompare(a);
}

function mergeSort(arr) {
    var len = arr.length;
    if (len < 2) {
        return arr;
    }
    var mid = Math.floor(len / 2),
        left = arr.slice(0, mid),
        right = arr.slice(mid);
    //send left and right to the mergeSort to broke it down into pieces
    //then merge those
    return merge(mergeSort(left), mergeSort(right));
}

function merge(left, right) {
    var result = [],
        lLen = left.length,
        rLen = right.length,
        l = 0,
        r = 0;
    while (l < lLen && r < rLen) {
        if (compare(left[l], right[r]) < 0) { // use comparing function and check
            result.push(left[l++]);           // against zero for smaller values
        } else {
            result.push(right[r++]);
        }
    }
    //remaining part needs to be addred to the result
    return result.concat(left.slice(l)).concat(right.slice(r));
}

console.log(mergeSort(['a', 'B', 'b', 'A', 'c']))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 2

Przemek Praca
Przemek Praca

Reputation: 36

All you need to do is changing if statement in line

if(left[l] < right[r])

to something that will check lowercased characters and case when lowercased characters are equal

if( left[l].toLowerCase() < right[r].toLowerCase() || (left[l].toLowerCase() == right[r].toLowerCase() && left[l] < right[r]) ) Best regards

Upvotes: 0

Related Questions