我如何按字母顺序排列JSON元素>使用localeCompare()降低1级?

时间:2016-08-31 13:57:24

标签: javascript string string-comparison

我刚刚使用sort()localeCompare()的组合为我正在使用的基于Javascript的应用创建了一个字母顺序功能。 JSON的每个部分都有一个属性“name”,以下代码使用:

  var alphabetize = function() {
    flatresponse.sort(function(a, b) {
      return a.name.localeCompare(b.name);
    });
    console.log(flatresponse); 
    });
  };
  alphabetize(); //A-Z sorted!

flatresponse对象示例:

  { workbookId: '148',
    updatedAt: '2016-02-10T12:45:24.707Z',
    usageInfo: 
     { hitsTotal: 2,
       hitsLastOneMonthTotal: 0,
       hitsLastThreeMonthsTotal: 0,
       hitsLastTwelveMonthsTotal: 0,
       favoritesTotal: 0 },
    thumbnailUrl: 'vizportal/api/rest/v1/views/753/thumbnail?1455108324707',
    name: 'Sales Forecast',
    id: '753',
    path: 'Sales/SalesForecast',
    index: '2',
    serverUrl: 'tableau.biztory.be',
    workbookName: 'Sales',
    siteName: '23Seconds' },
  { workbookId: '182',
    updatedAt: '2016-03-16T09:05:07.176Z',
    usageInfo: 
     { hitsTotal: 0,
       hitsLastOneMonthTotal: 0,
       hitsLastThreeMonthsTotal: 0,
       hitsLastTwelveMonthsTotal: 0,
       favoritesTotal: 0 },
    thumbnailUrl: 'vizportal/api/rest/v1/views/890/thumbnail?1458119107176',
    name: 'Sales Forecast',
    id: '890',
    path: 'WebServicesLayer/SalesForecast',
    index: '2',
    serverUrl: 'tableau.biztory.be',
    workbookName: 'Web Services Layer',
    siteName: '23Seconds' },
  { workbookId: '175',
    updatedAt: '2016-03-01T14:22:32.360Z',
    usageInfo: 
     { hitsTotal: 2,
       hitsLastOneMonthTotal: 0,
       hitsLastThreeMonthsTotal: 1,
       hitsLastTwelveMonthsTotal: 1,
       favoritesTotal: 0 },
    thumbnailUrl: 'vizportal/api/rest/v1/views/860/thumbnail?1456842152360',
    name: 'Sales per category',
    id: '860',
    path: 'ExampleBaxter/Salespercategory',
    index: '1',
    serverUrl: 'tableau.biztory.be',
    workbookName: 'Example Baxter',
    siteName: '23Seconds' }

我想知道如何针对树下的元素进行调整,例如:排序a.usageInfo.hitsTotal?或者任何元素x降级为此?

1 个答案:

答案 0 :(得分:0)

例如,您可以按

对数据进行排序
  • $.ajax({ url: 'php.php', success: function (data) { console.log(data); $('.container').html(data); } }).done(function( data) { console.log(data); $('.container').html(data); });

  • usageInfo.hitsTotal

都在下降。

usageInfo.hitsLastTwelveMonthsTotal
var data = [{ workbookId: '148', updatedAt: '2016-02-10T12:45:24.707Z', usageInfo: { hitsTotal: 2, hitsLastOneMonthTotal: 0, hitsLastThreeMonthsTotal: 0, hitsLastTwelveMonthsTotal: 0, favoritesTotal: 0 }, thumbnailUrl: 'vizportal/api/rest/v1/views/753/thumbnail?1455108324707', name: 'Sales Forecast', id: '753', path: 'Sales/SalesForecast', index: '2', serverUrl: 'tableau.biztory.be', workbookName: 'Sales', siteName: '23Seconds' }, { workbookId: '182', updatedAt: '2016-03-16T09:05:07.176Z', usageInfo: { hitsTotal: 0, hitsLastOneMonthTotal: 0, hitsLastThreeMonthsTotal: 0, hitsLastTwelveMonthsTotal: 0, favoritesTotal: 0 }, thumbnailUrl: 'vizportal/api/rest/v1/views/890/thumbnail?1458119107176', name: 'Sales Forecast', id: '890', path: 'WebServicesLayer/SalesForecast', index: '2', serverUrl: 'tableau.biztory.be', workbookName: 'Web Services Layer', siteName: '23Seconds' }, { workbookId: '175', updatedAt: '2016-03-01T14:22:32.360Z', usageInfo: { hitsTotal: 2, hitsLastOneMonthTotal: 0, hitsLastThreeMonthsTotal: 1, hitsLastTwelveMonthsTotal: 1, favoritesTotal: 0 }, thumbnailUrl: 'vizportal/api/rest/v1/views/860/thumbnail?1456842152360', name: 'Sales per category', id: '860', path: 'ExampleBaxter/Salespercategory', index: '1', serverUrl: 'tableau.biztory.be', workbookName: 'Example Baxter', siteName: '23Seconds' }];

data.sort(function (a, b) {
    return b.usageInfo.hitsTotal - a.usageInfo.hitsTotal || b.usageInfo.hitsLastTwelveMonthsTotal - a.usageInfo.hitsLastTwelveMonthsTotal;
});

console.log(data);