2016-06-21 31 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更快。 「重寫......」可能是最好的方式。 –