HISE Logo Forum
    • Categories
    • Register
    • Login

    Secret Array.sort() function argument lets you sort an array of objects by a property

    Scheduled Pinned Locked Moved Documentation
    2 Posts 2 Posters 124 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      aaronventure
      last edited by aaronventure

      inline function sortByProperty(arr, prop)
      {
          arr.sort(function[prop](a, b) 
          {
              if (a[prop] < b[prop]) 
                  return -1;
              if (a[prop] > b[prop]) 
                  return 1;
              return 0;
          });
          return arr;
      }
      
      var array = [
          {"name": "John", "age": 25},
          {"name": "Jane", "age": 22},
          {"name": "Joe", "age": 30},
          {"name": "Jack", "age": 3},
      ];
      
      Console.print(trace(sortByProperty(array, "age")));
      
      d.healeyD 1 Reply Last reply Reply Quote 5
      • d.healeyD
        d.healey @aaronventure
        last edited by

        @aaronventure Ooo that's sneaky

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        1 Reply Last reply Reply Quote 0
        • First post
          Last post

        31

        Online

        1.7k

        Users

        11.7k

        Topics

        102.3k

        Posts