75lb / sort-array Goto Github PK
View Code? Open in Web Editor NEWIsomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order
License: MIT License
Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order
License: MIT License
Low priority but would be nice to have some input validation which ensures the config supplied is valid. E.g. ensure a customOrder
is either a function or array of values. The following is invalid and should raise an "invalid config" exception:
sortArray(arr, {
by: 'something',
order: 'broken',
customOrders: {
broken: new Date()
}
})
So I have a JSON in the format
{"homepage_card.title":{"type":"StructuredText","value":[{"type":"heading1","text":"Test Card","spans":[]}]},"homepage_card.type":{"type":"Select","value":"default"},"homepage_card.sequence":{"type":"Number","value":4},"homepage_card.description":{"type":"StructuredText","value":[{"type":"paragraph","text":"This is some description","spans":[]}]},"homepage_card.background_image":{"type":"Image","value":{"main":{"dimensions":{"width":960,"height":800},"alt":null,"copyright":null,"url":"https://wroomdev.s3.amazonaws.com/tutoblanktemplate%2F97109f41-140e-4dc9-a2c8-96fb10f14051_star.gif"},"views":{}}},"homepage_card.label":{"type":"Text","value":"Label"},"homepage_card.rotating_cards":{"type":"Group","value":[{}]}}
where the keys have a dot.
It did not seem to work for this case when I was trying to sort homepage_card.sequence.value
I needed to sort a list by string ex: all items with the property "source"
if it equals to "Unlinked" it should drop to the end of the list
if it doesn't equals to "Unlinked" it should be shifted to start of the array
I managed to do this like this:
return sortArray(list, {
by: "source",
order,
computed: {
total: source => (source === "Unlinked" ? 1 : 0),
},
});
I suggest you add a support for that
Update customOrders
to also accept a standard sort compare function.
E.g.
sortArray(partsOfTheDay, {
order: 'time',
customOrders: {
time: (a, b) => return a > b ? 1 : -1 // etc
}
})
For example.
const records = [
{ a: 4, b: 1, c: 1 },
{ a: 4, b: 3, c: 1 },
{ a: 2, b: 2, c: 3 },
{ a: 2, b: 2, c: 2 },
{ a: 1, b: 3, c: 4 }
]
sort(records, [
[ 'a', 'desc' ],
[ 'b', 'asc' ],
[ 'c', 'desc' ]
])
Just found out about this library, great job!
reading the code, the sort happens in-place. It would be nice if I could leave the original array as is, and get a new sorted array
Sorting of this failed:
var ar = [[99, "a"], [1, "a"]];
sortArray(ar, {by:[1,0]});
I changed this line
if (result === 0 && by[byIndex + 1]) {
to this
if (result === 0 && (undefined !== by[byIndex + 1])) {
to fix it.
I'm using sortArray with an array that contains null values in field.
sortArray(myArray, {by: ['fieldA',fieldB']})
fieldA contains two values, null and the number "1". Regardless of how I sort the array (asc or desc), the null values are always at the end. I was expecting them to be first when using asc order but they are last in both cases.
For example
const fixture = [
{ a: 5, b: 10 },
{ a: 2, b: 10 },
{ a: 3, b: 10 },
{ a: 1, b: 10 },
{ a: 4, b: 10 }
]
const by = [
item => item.a + item.b
]
/* Sorts recordset by the result of `a + b` for each object in the array */
const result = sort(fixture, by)
Hi -- is there any way to control whether the sorting for a property is in ascending or descending order?
Need a options.NaNrank
option - same functionality as options.undefinedRank
and options.nullRank
but for NaN
values.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.