[6.x] Sort numbers numerically in indexed_array (#17113) (#17117)

This commit is contained in:
Spencer 2018-03-12 18:19:11 -07:00 committed by GitHub
parent 8ebcb3c65d
commit 3f9fee6017
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 7 deletions

View file

@ -5,7 +5,7 @@ import { IndexedArray } from 'ui/indexed_array';
// this is generally a data-structure that IndexedArray is good for managing
const users = [
{ name: 'John', id: 69, username: 'beast', group: 'admins' },
{ name: 'John', id: 6, username: 'beast', group: 'admins' },
{ name: 'Anon', id: 0, username: 'shhhh', group: 'secret' },
{ name: 'Fern', id: 42, username: 'kitty', group: 'editor' },
{ name: 'Mary', id: 55, username: 'sheep', group: 'editor' }
@ -170,9 +170,9 @@ describe('IndexedArray', function () {
const ordered = reg.inIdOrder;
expect(ordered[0].id).to.be(0);
expect(ordered[1].id).to.be(42);
expect(ordered[2].id).to.be(55);
expect(ordered[3].id).to.be(69);
expect(ordered[1].id).to.be(6);
expect(ordered[2].id).to.be(42);
expect(ordered[3].id).to.be(55);
});
});
});

View file

@ -35,9 +35,12 @@ export class IndexedArray {
this._setupIndex(config.index, inflectIndex, _.indexBy),
this._setupIndex(config.order, inflectOrder, (raw, pluckValue) => {
return [...raw].sort((itemA, itemB) => {
const a = String(pluckValue(itemA));
const b = String(pluckValue(itemB));
return a.toLowerCase().localeCompare(b.toLowerCase());
const a = pluckValue(itemA);
const b = pluckValue(itemB);
if (typeof a === 'number' && typeof b === 'number') {
return a - b;
}
return String(a).toLowerCase().localeCompare(String(b).toLowerCase());
});
})
);