sort

The sort function is built to be used with sorting tables, but since all it really does is sort an array of objects, you can use it anywhere. It works for both string and number fields out of the box, and you don't need to specify which is which. If there is an error, it will gracefully return the unsorted data and log out the error to the console.




Props


array Array<Object>
This is the data that you pass in.


sort_by string
This is the property on the object that you want to sort by. If the property is nested, you can use dot notation to access it like this "deeply.nested.prop"


order string
Options are "descending" or "ascending"



Returns


Array<Object>
Returns the sorted array


import { sort } from "bubbles-ui";

const data = [
  {
    id: 1,
    deeply: {
      nested: "a",
    },
  },
  {
    id: "2", //Doesn't matter if number is a string
    deeply: {
      nested: "b",
    },
  },
  {
    id: 3,
    deeply: {
      nested: "c",
    },
  },
];

const sorted = sort(data, "id", "descending");
const nested_sort = sort(data, "deeply.nested", "descending");