0
var sortedFeatures = features.sort(function(a,b){
    if(a.properties.Project_Year < b.properties.Project_Year) {return 1}
    if(a.properties.Project_Year > b.properties.Project_Year) {return -1 }
    return 0;
}) 

For now what this code does is it sorts my attributes by year which is the first part.

Now for the second part I have an attribute called Project_Name. I want to keep the files sorted by year, but within that sort I would like to also add sort by alphabetical order.

My output is already established. I would want to know how to adjust this code to get the proper sort i am looking for.

adiga
  • 34,372
  • 9
  • 61
  • 83
user35131
  • 1,105
  • 6
  • 18

3 Answers3

0

A good way to do this is to look at the return 0; part of your sort - this is what occurs when the Project_Year properties are both the same. If you replace this with an alphabetic comparison on the Project_Name property, you should get the answer you want:

var sortedFeatures = features.sort(function(a,b){
   if(a.properties.Project_Year < b.properties.Project_Year) {return 1}
   if(a.properties.Project_Year > b.properties.Project_Year) {return -1 }

   //compare names alphabetically
   return a.properties.Project_Name.localeCompare(b.properties.Project_Name);
});

Note that this doesn't handle the case where Project_Name isn't a valid string (e.g. undefined or null) and the program will throw an error in that case, so if that's a valid possibility then throw some if() statements in there to guard against it, e.g.

var sortedFeatures = features.sort(function(a,b){
   if(a.properties.Project_Year < b.properties.Project_Year) {return 1}
   if(a.properties.Project_Year > b.properties.Project_Year) {return -1 }

   //compare names alphabetically
   const projectNameA = a.properties.Project_Name || ""; //default to empty string
   const projectNameB = b.properties.Project_Name || ""; 
   return projectNameA.localeCompare(projectNameB);
});
Duncan Thacker
  • 5,073
  • 1
  • 10
  • 20
0

This compareFunction sorts the features by Project_Year in descending order first. If the years are same, the difference is zero. This falsy value forces it to check the second condition and compares based on the Project_Name

var features = [
  { properties : { Project_Name : 'B', Project_Year : 2018 } },
  { properties : { Project_Name : 'A', Project_Year : 2018 } },
  { properties : { Project_Name : 'C', Project_Year : 2018 } },
  { properties : { Project_Name : 'A', Project_Year : 2019 } },
  { properties : { Project_Name : 'B', Project_Year : 2019 } },
];

var sortedFeatures = features.sort(function(a, b) {
  return b.properties.Project_Year - a.properties.Project_Year 
          || a.properties.Project_Name.localeCompare(b.properties.Project_Name)
})

console.log(sortedFeatures)

This sorts by year in the descending order and then within the year, it sort based on the alphabetic order.

adiga
  • 34,372
  • 9
  • 61
  • 83
0

This is a fairly simple task. Just compare the years, if they are the same then you compare the names.

var features = [
  { Project_Name : 'C', properties : { Project_Year : new Date(2019, 1, 0) } },
  { Project_Name : 'A', properties : { Project_Year : new Date(2019, 1, 0) } },
  { Project_Name : 'B', properties : { Project_Year : new Date(2019, 1, 0) } },
  { Project_Name : 'C', properties : { Project_Year : new Date(2018, 1, 0) } },
  { Project_Name : 'A', properties : { Project_Year : new Date(2017, 1, 0) } },
  { Project_Name : 'B', properties : { Project_Year : new Date(2018, 1, 0) } }
];

/** Sort by year, followed by name */
var sortedFeatures = features.sort(function(a, b) {
  var yearDiff = a.properties.Project_Year - b.properties.Project_Year;
  if (yearDiff === 0) {
    return a.Project_Name.localeCompare(b.Project_Name);
  }
  return yearDiff;
});

console.log(sortedFeatures);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Dynamic approach

If you want a more generic implementation, you can check out this example. I created an extend implementation of this example which queries the scope of nested properties.

var sort_by;
(function() {
  var default_cmp = (a, b) => a == b ? 0 : (a < b ? -1 : 1);
  getCmpFunc = (primer, reverse) => {
    let dfc = default_cmp, cmp = default_cmp;
    if (primer) {
      cmp = (a, b) => dfc(primer(a), primer(b));
    }
    if (reverse) {
      return (a, b) => -1 * cmp(a, b);
    }
    return cmp;
  };
  queryValue = function(v, scope) {
    return scope == null || scope.length === 0 ? v : queryValue(v[scope.shift()], scope);
  };
  sort_by = function() {
    let fields = [], n_fields = arguments.length, field, name, reverse, cmp;
    for (var i = 0; i < n_fields; i++) {
      field = arguments[i];
      if (typeof field === 'string') {
        name = field;
        cmp = default_cmp;
      } else {
        name = field.name;
        cmp = getCmpFunc(field.primer, field.reverse);
      }
      fields.push({ name: name, cmp: cmp });
    }
    return (A, B) => {
      let a, b, result;
      for (var i = 0; i < n_fields; i++) {
        result = 0;
        a = queryValue(A, fields[i].name.split('.'));
        b = queryValue(B, fields[i].name.split('.'));
        result = fields[i].cmp(a, b);
        if (result !== 0) break;
      }
      return result;
    };
  };
}());

var features = [
  { Project_Name : 'C', properties : { Project_Year : new Date(2019, 1, 0) } },
  { Project_Name : 'A', properties : { Project_Year : new Date(2019, 1, 0) } },
  { Project_Name : 'B', properties : { Project_Year : new Date(2019, 1, 0) } },
  { Project_Name : 'C', properties : { Project_Year : new Date(2018, 1, 0) } },
  { Project_Name : 'A', properties : { Project_Year : new Date(2017, 1, 0) } },
  { Project_Name : 'B', properties : { Project_Year : new Date(2018, 1, 0) } }
];

/** Sort by year (decending), followed by name (ascending) */
var sortedFeatures = features.sort(sort_by(
  { name: 'properties.Project_Year', reverse: true, primer : (d) => d.getTime() },
  'Project_Name' // scalar string value
));

console.log(sortedFeatures);
.as-console-wrapper {
  top: 0;
  max-height: 100% !important;
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132