Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8899991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:59:37+00:00 2026-06-15T00:59:37+00:00

I was wondering if how to create a custom sortOn function, with some sort

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:59:39+00:00Added an answer on June 15, 2026 at 12:59 am

    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:

    function myCustomSort(firstObject, secondObject):int 
    {
        // Is firstObject a food and the second not? firstObject goes in front. 
        // Are both objects a food?
            // Is firstObject's name pizza and the second not? 
            // firstObject goes in front. 
            //Are both objects' names pizza? Return 0 - they're equivalent.
        //ETC.
    }
    

    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:

    priorityList = {
                        "consumingType":[
                          "food":{
                             "name":[
                                "pizza", 
                                "tea"
                                ], 
                             amount: Array.ASCENDING
                            }, 
                          "drink", 
                          "other"
                        ], 
                        amount: Array.ASCENDING
                    };   
    

    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:

    function handleComplexSorting( sortingObject, first, second ) {
        var result:int = 0;
        //Extracting the property name of first/second will differ, 
        //but pretty sure this is how you do it in AS3.
        var firstValue:int = findSortValueForProperty(first[0], sortingObject[0]);
        var secondValue:int = findSortValueForProperty(second[0], sortingObject[0]);
    
        if( firstValue > secondValue ) {
            //firstValue should go first.
            result = -1;
        }
        else if(firstValue < secondValue){
            //secondValue should go first.
            result = 1;
        }
        else {//equal
            var childSortingObject:Object;
            if( hasChildSorting(sortingObject, childSortingObject) ){
                //return result of recursion
                result = handleComplexSorting(childSortingObject, first, second);
            }
            else {
                result = 0;
            }
        }
        return result;
    }
    

    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).

    function findSortValueForProperty(property:String, priorityProperties:Array):int {
        var resultValue:int = 0;
        var index = 0;
        for each( var currentProperty:String in priorityProperties ) {
            if(property == currentProperty) {
                resultValue = priorityProperties.length - index;
                break;
            }
            ++index;
        }
        return resultValue;
    }
    

    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).

    function hasChildSorting(sortingObject:Object, outChild:Object):Boolean {
        var result:Boolean = false;
            if( sortingObject[0] is Object ) {
                result = true;
                outChild = sortingObject[0];
            }
        }
        return result;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if it is possible in PHP to create a custom break command/function.
For some time now I've been wondering whether I could create a custom template
I'm wondering what's the canonical way in Ruby to create custom setter and getter
I'm trying to create a custom MembershipProvider and I was wondering how I would
I am wondering how I could create a custom data type to use within
I am trying to create a custom wpf control, I'm wondering how I can
I was wondering whether there is any way to create custom Orders statuses in
I've always wanted to create custom components in Java, or customize existing ones, but
I'm trying to create a custom registration component for Joomla, and I was wondering
I'm planning to create a custom system for comments. I was wondering about comment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.