复杂的排序

时间:2016-04-18 13:22:36

标签: javascript sorting

我有很多结构,如:

student :
 name: SomeName
 city : someSity   
 school : someschool
 date : somedate

我想要的是以(使用测试数据)的方式对它们进行排序:

Sorted_map:
    city : NY
       school: School1
             name:Name1
             name: Name2
             name: Name4
       shool: School2
             name:Name11
             name: Name21
             name: Name41

    city: WDC  
        school: School3
           name: Name22  
           name: Name29 

我所做的是按城市排序:

function groupBy( array , f ) {
    var groups = {};
    array.forEach( function( o )
    {
        var group = JSON.stringify( f(o) );
        groups[group] = groups[group] || [];
        groups[group].push( o );
    });
     return groups;    
}


var result = groupBy(sudentsItems, function(item)
    {

        return unifyCity(item.city);
    }

所以现在它只按" city"键。如何在城市内对数据进行排序?

我想我需要像Map< city_key,地图< school_key,name>取代;

数据样本:

[{
    "name":   "John Some",
    "city":   "NY",
    "school": "NY Central",
    "date":   1460986261733
}, {
    "name":   "Sarah Mil",
    "city":   "Moscow",
    "school": "Moscow Central",
    "date":   1460986201733
}, {
    "name":   "John Again",
    "city":   "NY",
    "school": "NY Central",
    "date":   1460986261733
}, {
    "name":   "Kit Parcer",
    "city":   "NY",
    "school": "NY Central",
    "date":   1460086261733
},  {
    "name":   "Anna Love",
    "city":   "SPB",
    "school": "SPB Central",
    "date":   1460983261733
}]

2 个答案:

答案 0 :(得分:2)

看起来您正在查找已排序或分组的数据。



var data = [{ "name": "John Some", "city": "NY", "school": "NY Central", "date": 1460986261733 }, { "name": "Sarah Mil", "city": "Moscow", "school": "Moscow Central", "date": 1460986201733 }, { "name": "John Again", "city": "NY", "school": "NY Central", "date": 1460986261733 }, { "name": "Kit Parcer", "city": "NY", "school": "NY Central", "date": 1460086261733 }, { "name": "Anna Love", "city": "SPB", "school": "SPB Central", "date": 1460983261733 }],
    object = {};

data.sort(function (a,b) {
    return a.city.localeCompare(b.city) || 
        a.school.localeCompare(b.school) || 
        a.name.localeCompare(b.name);
});

data.forEach(function (a) {
    object[a.city] = object[a.city] || {};
    object[a.city][a.school] = object[a.city][a.school] || [];
    object[a.city][a.school].push(a.name);
});

document.write('<pre>data sorted: ' + JSON.stringify(data, 0, 4) + '</pre>');
document.write('<pre>data grouped: ' + JSON.stringify(object, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用for循环迭代分拣器数组中的所有字段。如果在任何时候,比较结果不是0(等于),则立即返回该值。

&#13;
&#13;
function sortMulti(data, sorters) {
  return data.sort(function(a, b) {
    for (var i = 0; i < sorters.length; i++) {
      var result = 0;
      var f1 = a[sorters[i].field];
      var f2 = b[sorters[i].field];
      if (typeof f1 === 'string' || typeof f2 === 'string') {
        result = f1.localeCompare(f2);
      } else {
        result = f1 < f2 ? -1 : f1 > f2 ? 1 : 0;
      }
      if (sorters[i].direction === 'desc') {
        result = result * -1;
      }
      if (result !== 0) {
        return result;
      }
    }
  });
}

var data = [
  { "name": "John Some",  "city": "NY",     "school": "NY Central",     "date": 1460986261733 },
  { "name": "Sarah Mil",  "city": "Moscow", "school": "Moscow Central", "date": 1460986201733 },
  { "name": "John Again", "city": "NY",     "school": "NY Central",     "date": 1460986261733 },
  { "name": "Kit Parcer", "city": "NY",     "school": "NY Central",     "date": 1460086261733 },
  { "name": "Anna Love",  "city": "SPB",    "school": "SPB Central",    "date": 1460983261733 }
];

var sorters = [
  { field: 'city',   direction: 'asc' },
  { field: 'school', direction: 'asc' },
  { field: 'name',   direction: 'asc' }
];

document.body.innerHTML = '<pre>' + JSON.stringify(sortMulti(data, sorters), null, 4) + '</pre>';
&#13;
&#13;
&#13;