Как отсортировать месячный заказ в javascript [дубликат]

Вот простое решение, которое использует forEach (возвращается к IE9):

var funcs = {};
[0,1,2].forEach(function(i) {          // let's create 3 functions
    funcs[i] = function() {            // and store them in funcs
        console.log("My value: " + i); // each should log its value.
    };
})
for (var j = 0; j < 3; j++) {
    funcs[j]();                        // and now let's run each one to see
}

Печатает:

My value: 0
My value: 1
My value: 2
1
задан A191919 20 May 2016 в 14:26
поделиться

1 ответ

Вы можете сделать это, указав список всех месяцев в правильном порядке и отсортировав свой массив на основе них:

var dataCollection = [
  { values: { Month: { displayValue: "August" }, Sum: "10" } },
  { values: { Month: { displayValue: "February" }, Sum: "25" } },
  { values: { Month: { displayValue: "July" }, Sum: "35" } }
];

sortByMonth(dataCollection);

console.log(dataCollection);

function sortByMonth(arr) {
  var months = ["January", "February", "March", "April", "May", "June",
  	        "July", "August", "September", "October", "November", "December"];
  arr.sort(function(a, b){
      return months.indexOf(a.values.Month.displayValue)
           - months.indexOf(b.values.Month.displayValue);
  });
}

6
ответ дан blex 4 September 2018 в 08:06
поделиться
Другие вопросы по тегам:

Похожие вопросы: