6

I have some code using JavaScript Array Sorting that works but seems to be really inefficient.

I have an array of objects defined as follows for example purposes

dummyData = [];
dummyData.push({ col01:"aa", col02:"ac", col03:"ab" });
dummyData.push({ col01:"ab", col02:"ab", col03:"ac" });
dummyData.push({ col01:"ac", col02:"aa", col03:"aa" });

Which I can then sort on col01 using a function like this

function f_sort_col01(dataArg) {

   dataArg.sort(function(res01, res02) {
      var arg01 = res01.col01.toLowerCase();
      var arg02 = res02.col01.toLowerCase();
      if(arg01 < arg02) { return -1; }
      if(arg01 > arg02) { return 1; }
      return 0;
   });

   return dataArg;

}

This works just fine but the problem is that when I need to sort on a different column I then have to create an entire new function like this

function f_sort_col02(dataArg) {

   dataArg.sort(function(res01, res02) {
      var arg01 = res01.col02.toLowerCase();
      var arg02 = res02.col02.toLowerCase();
      if(arg01 < arg02) { return -1; }
      if(arg01 > arg02) { return 1; }
      return 0;
   });

   return dataArg;

}

Which is pretty much the same thing only on a different column. I was wondering if it's possible to do something along the lines of this

function f_sort(dataArg, colName) {

   dataArg.sort(function(res01, res02) {
      var arg01 = res01.colName.toLowerCase();
      var arg02 = res02.colName.toLowerCase();
      if(arg01 < arg02) { return -1; }
      if(arg01 > arg02) { return 1; }
      return 0;
   });

   return dataArg;

}

So that the name of the column can be included in the parameters

0

4 Answers 4

7

Use square brackets like following

function f_sort(dataArg, colName) {

   dataArg.sort(function(res01, res02) {
      var arg01 = res01[colName].toLowerCase();
      var arg02 = res02[colName].toLowerCase();
      if(arg01 < arg02) { return -1; }
      if(arg01 > arg02) { return 1; }
      return 0;
   });
   return dataArg;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, I'm an idiot, thank you so much, what a relief to have such a nice simple answer, I will accept this as the answer as soon as it let's me
@TheLovelySausage - Glad to help you!
4

You coud reverse the approach by using a closure over the wanted key.

function sortBy(key) {
    return function (a, b) {
        return a[key].toLowerCase().localeCompare(b[key].toLowerCase());
    };
}

var data = [{ col01: "aa", col02: "ac", col03: "ab" }, { col01: "ab", col02: "ab", col03: "ac" }, { col01: "ac", col02: "aa", col03: "aa" }];

console.log(data.sort(sortBy('col01')));
console.log(data.sort(sortBy('col02')));
console.log(data.sort(sortBy('col03')));
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

This is super nifty code as well, I like it a lot
3

Use [colName] instead of .colName

function f_sort(dataArg, colName) {

   dataArg.sort(function(res01, res02) {
      var arg01 = res01[colName].toLowerCase();
      var arg02 = res02[colName].toLowerCase();
      if(arg01 < arg02) { return -1; }
      if(arg01 > arg02) { return 1; }
      return 0;
   });

   return dataArg;

}

Comments

1

Square bracket notation:

var arg01 = res01[colName].toLowerCase();
var arg02 = res02[colName].toLowerCase();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.