Giter VIP home page Giter VIP logo

sort-array's Issues

Add input validation

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()
  }
})

Does it work if the key has dot character ?

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

Suggestion

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

add the ability to sort in a new array

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

Bugfix

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.

How are NULL values handled?

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.

Add ability to sort by a computed property value

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)

reverse sorting?

Hi -- is there any way to control whether the sorting for a property is in ascending or descending order?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.