sort array of objects by variable obj property

javascript sort array of objects by key
javascript sort array of objects by number
javascript sort array of objects by key value
javascript sort array of objects by string property
angular 6 sort array of objects by property
sort array of objects javascript
lodash sort array of objects
javascript sort object by key

I am sorting an array of objects by object property using this process:

function cmp($a, $b)
{
    return strcmp($a->name, $b->name);
}

usort($array_of_obj, "cmp"); //sorts array by object name

In my case, the object property is stored as in variable $prop, so that I can choose which property to sort from (name, date, etc). So, I would like something like this:

function cmp($a, $b, $prop)  
{
    return strcmp($a->$prop, $b->$prop);
}

$prop = 'someproperty';
usort($array_of_obj, "cmp");  //sorts array by chosen object property, $prop

My problem here is that I cannot pass a value to the $prop argument when I call the "cmp" function. I'm not sure if I should if this is a dead end endeavor or if there is some way to work around this. What can I do?


You could wrap the call inside an anonymous function

function cmp($a, $b, $prop) {
    return strcmp($a->$prop, $b->$prop);
}

$prop = 'someproperty';
usort($array_of_obj, function($a,$b) use ($prop) { 
    return cmp($a,$b,$prop); 
});

EDIT: Explanation of the keyword 'use'

Sort array of objects by string property value, If you have an array of objects that you need to sort into a certain order, you such as declaring variables, writing functions, and conditional statements. hasOwnProperty(key)) { // property doesn't exist on either object return  In the main() method, we've created an array list of custom objects list, initialized with 5 objects. For sorting the list with the given property, we use list's sort() method. The sort() method takes the list to be sorted (final sorted list is also the same) and a comparator.


You could add a static property to the class $a & $b belong to or a shared parent class. You can call it something like 'sort_property', and then use that:

   //Set the sort property 
   Class_of_a_and_b::$sort_property = 'name';

   //call sort
   usort($array_of_obj, "cmp");

   //....stuff ...

   function cmp($a, $b)
   {
         //in real code, maybe test if the sort property is valid...
         $sort_prop = Class_of_a_and_b::$sort_property;
         return strcmp($a->$sort_prop , $b->$sort_prop );
   }

Of course this only works well if they're objects of the same class.

Quick Tip: How to Sort an Array of Objects in JavaScript, The employees array above is an array of objects with properties of different to sort using is deep inside a property of the array object, and not the array itself. You want to render this list, but first you want to order it by the value of one of the properties. For example you want to order it by the color name, in alphabetical order: black, red, white. You can use the sort() method of Array, which takes a callback function,


Just an idea

// ===============================================
// Start => Sort Object / Array by Key ===========
// ===============================================
function usort_array($array, $key, $order = 'asc') {
    if(strtolower($order) == 'asc')
        usort($array, function($a, $b) use ($key) { return strcmp($a[$key], $b[$key]); });
    else
        usort($array, function($a, $b) use ($key) { return strcmp($b[$key], $a[$key]); });
    return $array;
}

function usort_object($object, $key, $order = 'asc') {
    if(strtolower($order) == 'asc')
        usort($object, function($a, $b) use ($key) { return strcmp($a->$key, $b->$key); });
    else
        usort($object, function($a, $b) use ($key) { return strcmp($b->$key, $a->$key); });
    return $object;
}
// ===============================================
// End => Sort Object / Array by Key =============
// ===============================================

Sorting an array of objects, Arrays in JavaScript come with a built-in function that is​ used to sort elements be passed to the built-in sort() method to sort objects based on the specified property, console.log("Object to be sorted"); Notice that, to flip the rules that have been defined for sorting in ascending order, the sort_order variable is set to -1. function dynamicSortMultiple() { /* * save the arguments object as it will be overwritten * note that arguments object is an array-like object * consisting of the names of the properties to sort by */ var props = arguments; return function (obj1, obj2) { var i = 0, result = 0,


How to sort an array of objects in JavaScript, This tutorial shows you how to use the JavaScript array sort method to sort arrays of numbers, arrays of strings, and arrays of objects by a property. To resolve this, you use the localeCompare() method of the String object to compare strings in a specific locale, like this: Syntax · Variables · Data Types · Primitive vs. The Sort-Object cmdlet sorts objects based on properties specified in the command or the default sort properties for the object type. If an object does not have one of the specified properties, the property value for that object is interpreted by Sort-Object as Null and placed at the end of the sort order.


JavaScript Array sort: Sorting an Array More Effectively, The array of objects can be sort by using user defined function. This function Example 1: This example sorts the array of objects by l_name property. sort. </ button >. < p id = "GFG" ></ p >. <!-- Script to compare the object and. sort its  The people array in the above example contains objects of Person class. You cannot use Array.Sort(people) because array contains objects, not primitive values. Now, let's sort the above people array by the LastName property. For that, you need to create a class and implement IComparer interface, as shown below.


Sort array of objects by string property value in JavaScript , You can use it to sort an array in descending order: Sorting Object Arrays Even if objects have properties of different data types, the sort() method can be  Sort an Array of Objects by Property Using sort(fn) Building Resilient Systems on AWS : Learn how to design and implement a resilient, highly available, fault-tolerant infrastructure on AWS. By David Walsh on February 27, 2012