1

I have a custom sort function defined as below

sortArrayBy: function(a, b, sortKey) {
    if (a[sortKey] < b[sortKey])
        return -1;
    if (a[sortKey] > b[sortKey])
        return 1;
    return 0;
},

How can I update it to dynamically sort/toggle based on an additional parameter isAscending which can be true/false

So the function signature would look like

sortArrayBy: function(a, b, sortKey, isAscending) {

}
2
  • Can you please add an example of expected Input & Output? Commented Jan 30, 2018 at 11:18
  • 1
    what about the last question without any data. it is the same question. Commented Jan 30, 2018 at 11:52

2 Answers 2

5

Convert isAscending to a 1 (for ascending) or -1 (for descending)

sortArrayBy: function(a, b, sortKey, isAscending) {
   return (a[sortKey] - b[sortKey]) * (isAscending ? 1 : -1);
}

Demo

function sortArrayBy(arr, sortKey, isAscending) {
  return arr.sort( ( a, b ) => (a[sortKey] - b[sortKey]) * (isAscending ? 1 : -1) );
}

var arr = [
   { a : 1, b: 2 },
   { a : 4, b: 6 },
   { a : 3, b: 4 },
   { a : 12, b: 1 },
   { a : 5, b: 23 },
];

console.log( sortArrayBy( arr, "a", true ) );

console.log( sortArrayBy( arr, "a", false ) );

Edit

Including string comparison as well

function sortArrayBy(arr, type, sortKey, isAscending) {
  if ( type == "string" )
  {
     return arr.sort( ( a, b ) => a[sortKey].localeCompare(b[sortKey]) * (isAscending ? 1 : -1) );
  }

  return arr.sort( ( a, b ) => (a[sortKey] - b[sortKey]) * (isAscending ? 1 : -1) );
}

function sortArrayBy(arr, type, sortKey, isAscending) {
  if ( type == "string" )
  {
     return arr.sort( ( a, b ) => a[sortKey].localeCompare(b[sortKey]) * (isAscending ? 1 : -1) );
  }
  
  return arr.sort( ( a, b ) => (a[sortKey] - b[sortKey]) * (isAscending ? 1 : -1) );
}

var arr = [
   { a : 1, b: "32" },
   { a : 4, b: "w6" },
   { a : 3, b: "s4" },
   { a : 12, b: "v1" },
   { a : 5, b: "2s3" },
];

console.log( sortArrayBy( arr, "", "a", true ) );

console.log( sortArrayBy( arr, "", "a", false ) );


console.log( sortArrayBy( arr, "string", "b", true ) );

console.log( sortArrayBy( arr, "string", "b", false ) );

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

6 Comments

For some reasons, returning the same data set when isAscending is 1 or -1
@testndtv Please share some sample data-sets in your question.
@testndtv I have shared a demo.
Thanks..yes, I was passing 1 or -1 from the calling function itself...But can we extend this function to sort both alpha & numeric values...I want to srt in a grid which can have values like "abc", "xyz" and also some columns can have 1,2
Sure, give it a try and post a new question if you are stuck somewhere.
|
1

easy one

sortArrayBy: function(a, b, sortKey , isAscending) {
    if (parseFloat(a[sortKey]) < parseFloat(b[sortKey]))
        return isAscending ? -1 : 1;
    if (parseFloat(a[sortKey]) > parseFloat(b[sortKey]))
        return isAscending ? 1 : -1;
    return 0;
},

3 Comments

Thanks. Will this support both string & number data ?
Yes it should be
Does not sort the below 3 data properly "111.00", "55.00", "120.00"

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.