2

I am trying to sort my columns based on 'Value' field which is combination of strings and numerical values in the following order

var ascending = ["","","", 0, 0, 0.71, 1]

var descending = [1, 0.71,0,0,"","",""]

I tried the below sorting algorithm, but it is not giving desired results

function mySort(v1,v2) {
var v1 = obj1[colName];
            var v2 = obj2[colName];

            if(v1 === ""){
              return 1;
            }
            else if(v2 === ""){
              return -1;
            }
            else if(v1 === v2){
              return 0;
            }
            else if(!sortObj.descending) {
              return v1 < v2 ? -1 : 1;
            }
            else if(sortObj.descending) {
              return v1 < v2 ? 1 : -1;
            }
}

Above sorting algorithm always keeps the "" at bottom, which is not desired in my case. What I am missing?

4
  • Do you need to preserve the quotes, or is it ok if those values are coerced to a Number (0)? Because ["", "", 0, "", 1, 0.71, 0].map(Number).sort() works fine Commented Oct 21, 2016 at 19:04
  • yes, empty strings are to be preserved and different from zeros. That's where the default sort i tried first is getting confused and not producing desired order Commented Oct 21, 2016 at 19:07
  • Make the return value of your "" comparisons dependant on the sortObj.descending, as in return sortObj.descending ? 1 : -1, and the opposite for the second "". Commented Oct 21, 2016 at 19:10
  • @squint - Making the return value of your "" comparisons dependant on the sortObj.descending worked with my above code. Thanks for the input Commented Oct 26, 2016 at 15:28

3 Answers 3

1
var arr = ["", "", 0, "", 1, 0.71, 0];
var asc = arr.slice().sort();
var desc = asc.slice().reverse();

The slice() method returns a shallow copy of a portion of an array into a new array object.

The sort() method sorts the elements of an array in place and returns the array.

The reverse() method reverses an array in place.

Sign up to request clarification or add additional context in comments.

9 Comments

Hi it works if my input is just an array. I am trying to perform sort on column fields, in which case this will not work
An answer the has only peace of code is not a good answer. Add some explanation/links to sources...
@nikitha, what do you mean by "columns"? There is no "column" definition in javascript. Please explain.
This sorts everything as strings, so it'll fail to give the expected order in some cases.
A lexical sort will put "2" ahead of "10", for example.
|
1

You could sort first by empty strings, then the numbers.

var arr = ["", "", 0, "", 1, 0.71, 0, 10, 20, 2];

arr.sort(function (a, b) {
    return (a !== "") - (b !== "") || (a || 0) - (b || 0);
});
console.log(arr);

Comments

0

Remove those IF statements from your function.

    if(v1 === ""){
      return 1;
    }
    else if(v2 === ""){
      return -1;
    }

Numbers and strings like this are already comparable in JS.

Edit:

To compare 0 you can try this in the beginning of the function:

if(v1 === 0)
    v1 = v1.toString();
if(v2 === 0)
    v2 = v2.toString();
// the rest of your logic here

You can extract it to a method too, so you do not repeat code.

4 Comments

But, how does it compare "" and zeros?
@nikitha: They're numerically equal, so you won't be guaranteed to have all the strings grouped together.
Is there a way this to have this strings grouped. I am unable to achieve with my sort function
@nikitha check my edit, and you will be able to overcome this impediment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.