Reputation: 841
I have a json data like this
[ { 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' } ]
and i want to sort like
so on is that possible in json list to sort like numeric first then capital letters with alphabetic and rest alphabetically.
Upvotes: 0
Views: 169
Reputation: 280
Assuming that you pretend to order it directly in code (and not in the data source like a database) you can write some simple code to do it.
First let's start with the basic compare function
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));
}
After building the basic sorting function you can sort any object. Take your array of objects as an example:
const groups = //your groups here;
const sorted = groups.sort( (a, b) => compareString(a.name, b.name));
I'm using some ES6 syntax, let me know if you have any questions.
EDIT: I'm now on a car (not driving), I will explain the full code later.
EDIT2: Got this order using the code above (only with group names)
[ '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: While parking the car I've got an epiphany and realized that what you want is really the default string ordering of javascript. I was so focused on your problem and I forgot completely what I was doing. Anyway, I will let the code above as a reference. But you can use this code to sort your array
const sorted = groups.sort( (a, b) => a.name > b.name ? 1 : -1 )
How simple can it be? Basically it uses the same strategy of the code above, just compares the ASCII codes of the strings. Take a look at the table to check the sorting priority of the function.
Things may get nasty with UTF8 surrogate pairs though
Upvotes: 1