2016-06-21 70 views
0

我有这样如何将json数据首先用数字,第二大写字母和字母顺序排序,然后按字母顺序排序?

[ { groupType: '1', 
    id: '158', 
    unreadMessages: '8', 
    ownerId: '332', 
    name: 'porras group' }, 
    { groupType: '1', 
    id: '163', 
    unreadMessages: '0', 
    ownerId: '337', 
    name: '11..' }, 
    { groupType: '1', 
    id: '173', 
    unreadMessages: '0', 
    ownerId: '334', 
    name: 'cate\'s' }, 
    { groupType: '1', 
    id: '174', 
    unreadMessages: '0', 
    ownerId: '328', 
    name: 'raju' }, 
    { groupType: '1', 
    id: '175', 
    unreadMessages: '0', 
    ownerId: '332', 
    name: 'abcde' }, 
    { groupType: '1', 
    id: '177', 
    unreadMessages: '0', 
    ownerId: '337', 
    name: '26 feb' }, 
    { groupType: '1', 
    id: '181', 
    unreadMessages: '0', 
    ownerId: '332', 
    name: 'new' }, 
    { groupType: '1', 
    id: '182', 
    unreadMessages: '0', 
    ownerId: '337', 
    name: 'jchhabra group' }, 
    { groupType: '1', 
    id: '186', 
    unreadMessages: '0', 
    ownerId: '337', 
    name: 'jch' }, 
    { groupType: '1', 
    id: '189', 
    unreadMessages: '0', 
    ownerId: '332', 
    name: 'hebe' }, 
    { groupType: '1', 
    id: '191', 
    unreadMessages: '0', 
    ownerId: '328', 
    name: 'ccgg' }, 
    { groupType: '1', 
    id: '202', 
    unreadMessages: '0', 
    ownerId: '332', 
    name: 'New Porras Group' }, 
    { groupType: '1', 
    id: '205', 
    unreadMessages: '0', 
    ownerId: '339', 
    name: 'simgroup' }, 
    { groupType: '1', 
    id: '210', 
    unreadMessages: '0', 
    ownerId: '339', 
    name: 'check' }, 
    { groupType: '1', 
    id: '222', 
    unreadMessages: '1', 
    ownerId: '333', 
    name: 'jgonzalez group' }, 
    { groupType: '1', 
    id: '223', 
    unreadMessages: '0', 
    ownerId: '334', 
    name: 'Cate 2' }, 
    { groupType: '2', 
    id: '150', 
    unreadMessages: '0', 
    ownerId: '0', 
    name: 'BACKSTAFF Group 2' }, 
    { groupType: '2', 
    id: '158', 
    unreadMessages: '0', 
    ownerId: '0', 
    name: 'BACKSTAFF Group' }, 
    { groupType: '2', 
    id: '173', 
    unreadMessages: '0', 
    ownerId: '0', 
    name: 'BACKSTAFF Group 3' } ] 

一个JSON数据,我想有点像

  • 11 ..
  • 2月26日
  • BACKSTAFF集团
  • BACKSTAFF组2
  • BACKSTAFF Group 3
  • Cate 2
  • 新波拉斯集团
  • ABCDE
  • 美食的

等是可能的JSON列表进行排序像字母 数字第一则大写字母和休息 按字母顺序排列。

+1

是这个问题有关的任何特定的编程语言?如果没有在http://codegolf.stackexchange.com/询问,请点击这里 – Kira

+0

这是在node.js –

+0

你想重新排序集合中的属性或对象吗?如果属性是问题,我认为您需要更改类中的属性顺序,然后重新进行串行化。如果集合中的对象是你想要排序的东西,你可以反序列化,并使用.OrderBy()和再次reserialize – meJustAndrew

回答

1

假设您假装直接在代码(而不是像数据库中的数据源)中直接订购它,您可以编写一些简单的代码来完成它。

首先让我们先从基本的比较函数

function compareString(a, b) { 
    if (!(a && b)) return Math.sign(a.length - b.length); 

    const ca = a.codePointAt(0); 
    const cb = b.codePointAt(0); 
    const cmp = Math.sign(ca - cb); 

    return cmp ? cmp : compareString(a.slice(1), b.slice(1)); 
} 

建设的基本排序功能可以排序任何对象后。以您的一系列对象为例:

const groups = //your groups here; 
const sorted = groups.sort((a, b) => compareString(a.name, b.name)); 

我使用了一些ES6语法,如果您有任何问题,请告诉我。

编辑:我现在在一辆车上(不开车),我会在稍后解释完整的代码。

EDIT2:得到这个顺序使用上面的代码(仅适用于组名)

[ '11..', 
    '26 feb', 
    'BACKSTAFF Group', 
    'BACKSTAFF Group 2', 
    'BACKSTAFF Group 3', 
    'Cate 2', 
    'New Porras Group', 
    'abcde', 
    'cate\'s', 
    'ccgg', 
    'check', 
    'hebe', 
    'jch', 
    'jchhabra group', 
    'jgonzalez group', 
    'new', 
    'porras group', 
    'raju', 
    'simgroup' ] 

EDIT3:虽然停放汽车的我有一种顿悟和认识到你想要的是真正的默认字符串排序的JavaScript。我非常关注你的问题,我完全忘记了自己在做什么。无论如何,我会让上面的代码作为参考。但是您可以使用此代码对阵列进行排序

const sorted = groups.sort((a, b) => a.name > b.name ? 1 : -1) 

它有多简单?基本上它使用上面代码的相同策略,只是比较字符串的ASCII码。查看table来检查函数的排序优先级。

事情可能会变得讨厌与UTF8代理对虽然

+0

虽然它需要更多的代码,但我会使用第一种方法,但它更具前瞻性。现在你可能不需要支持比ASCII更多的东西,但在将来你可能会支持。如果您想将您的代码本地化为其他语言,许多非英语语言(特别是亚洲语言)会大量使用代理对。有关代码点和Unicode的更多信息,我会推荐[本次讨论](https://youtu.be/zi0w7J7MCrk)。 –

+0

嗯...实际上,因为你在你的递归中使用了'slice',当你碰到一个代理对时''''console.log(''。slice(1));'。你也许可以用'u'标志来使用RegExp来模拟切片:'console.log(/.(.*)/ u.exec('')[1]);'(这需要Node 6.0或更高版本对'u'标志的支持),或者重写它以使用['for ... of ...](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)。 ..of)循环而不是递归。 –

+0

RegExp选项可能会很慢,对于排序函数来说不是一件好事,特别是如果被排序的数据量很大。另一个选择是使用spread操作符将其转换为数组,然后切片数组而不是字符串:'console.log([...'']。slice(1).join('')); '。性能测试可能需要完成,我怀疑这也会很慢,但可能比RegExp更快。 “重写......”可能是最好的方式。 –