I was wondering if how to create a custom sortOn function, with some sort of sort-priorities. I have a more complex case, but I’m mostly wondering how to build nice sort functions.
Lets say I have this unsorted list with data objects:
var myList = [
{drink: {amount: 1, name: "cola"}},
{drink: {amount: 5, name: "beer"}},
{food: {amount: 7, name: "cake"}},
{drink: {amount: 3, name: "fanta"}},
{drink: {amount: 4, name: "tea}},
{other: {amount: 1, name: "table"}},
{food: {amount: 4, name: "mars"}},
{food: {amount: 5, name: "pizza"}},
{food: {amount: 4, name: "cake"}},
{other: {amount: 12, name: "chair"}},
{food: {amount: 14, name: "chips"}},
{drink: {amount: 6, name: "coffee}},
{food: {amount: 8, name: "chips"}},
{food: {amount: 6, name: "pizza"}},
{food: {amount: 1, name: "food"}}
]
Ok, imaging I want sort using these rules:
first: sort in this order: food, drinks, others (note, thats not alphabetical)
then: sort on amount, BUT on food, pizza's + cakes must be on top
In the ideal case it would look like this (manually created):
var myList = [
{food: {amount: 5, name: "pizza"}},
{food: {amount: 6, name: "pizza"}},
{food: {amount: 4, name: "cake"}},
{food: {amount: 7, name: "cake"}},
{food: {amount: 1, name: "food"}},
{food: {amount: 4, name: "mars"}},
{food: {amount: 8, name: "chips"}},
{food: {amount: 14, name: "chips"}},
{drink: {amount: 1, name: "cola"}},
{drink: {amount: 3, name: "fanta"}},
{drink: {amount: 4, name: "tea}},
{drink: {amount: 5, name: "beer"}},
{drink: {amount: 6, name: "coffee}},
{other: {amount: 1, name: "table"}},
{other: {amount: 12, name: "chair"}}
]
Again, this is totally not a real-world example, but I have cases with (even deeper nested objects) where I want to apply such rules.
Would it be possible to create some sort order look-up list (something like this), or is that not feasible?
priorityList = [
food:[
name:["pizza", "tea", rest],
amount: Array.ASCENDING}
],
drink,
other
]; // etc..
I want to learn how to make such kind of sort functions. I love to see a Javascript or Actionscript solution but other languages to illustrate are okay too. Is there a library for this? Do I have to loop through all items, create conditions, push/unshift in cases or is this possible with a custom sort() function? I need some directions how to solve this in a practical/efficient way.
Thanks in advance!
You’re right in that you’ll have to return -1, 0, or 1 but it doesn’t mean you cant handle your complex sorting inside it, though it’d mean you’d have to custom code for every case you encountered. Something like this would work:
Ideally though you’d want to support something like that schema you gave as the example. I’d recommend making the structure the same at the root as it is in the child (so you can run the same code on every level), your example would then look something like:
The actual solution may differ slightly depending on actual structure (whether your data is in JSON, or Objects) and what language – but here’s a starter in AS3 that I haven’t tested; that should point you in the right direction.
Firstly, I’d write a recursive sort function that handles one layer of the sorting, then checks if there’s a child layer or returns:
The findSortValueForProperty function would be where you’d add the ascending/descending, I’ve left it out, but you’d just need to check which one and invert the result value. The value here is determined by the inverse of its index in the array (first appearing has highest value, last has lowest, not there – 0 value).
Finally, a check to see if the object has child sorting requirements. This actually won’t work, it only expects the first item to maybe have child-sorting. I’m also not 100% that you can access an Object’s variables by index like I am (
sortingObject[0]) (you can definitely iterate over them though).For AS3 specifically see get string representation of a variable name in as3 for accessing variables by string.
You’ll also need your language to have reflection if you want to do it in something else.