Sorting a list of Javascript objects by object

As a newbie to programming, he was asked to implement the following script using Javascript (only):

  • Create a car object with properties (maximum speed, make, etc.)
  • Sort the list of cars sorted by speed, vintage ...

How should I do it?

+75
javascript sorting list oop properties
Mar 17 '10 at 22:26
source share
7 answers

javascript has a sort function that can take another function as a parameter - this second function is used to compare two elements.

Example:

cars = [ { name: "Honda", speed: 80 }, { name: "BMW", speed: 180 }, { name: "Trabi", speed: 40 }, { name: "Ferrari", speed: 200 } ] cars.sort(function(a, b) { return a.speed - b.speed; }) for(var i in cars) document.writeln(cars[i].name) // Trabi Honda BMW Ferrari 

ok, from your comment I see that you use the word "sort" in the wrong sense. In programming, "sorting" means "putting things in a specific order," rather than "sorting things in groups." The latter is much simpler - this is how you "sort" things in the real world.

  • make two empty arrays ("boxes")
  • for each item in your list, check if it matches the criteria
  • if yes, put it in the first "box"
  • if not, put it in the second “box”
+147
Mar 17 '10 at 22:36
source share

Example.

This works on cscript.exe, on windows.

 // define the Car class (function() { // makeClass - By John Resig (MIT Licensed) // Allows either new User() or User() to be employed for construction. function makeClass(){ return function(args){ if ( this instanceof arguments.callee ) { if ( typeof this.init == "function" ) this.init.apply( this, (args && args.callee) ? args : arguments ); } else return new arguments.callee( arguments ); }; } Car = makeClass(); Car.prototype.init = function(make, model, price, topSpeed, weight) { this.make = make; this.model = model; this.price = price; this.weight = weight; this.topSpeed = topSpeed; }; })(); // create a list of cars var autos = [ new Car("Chevy", "Corvair", 1800, 88, 2900), new Car("Buick", "LeSabre", 31000, 138, 3700), new Car("Toyota", "Prius", 24000, 103, 3200), new Car("Porsche", "911", 92000, 155, 3100), new Car("Mercedes", "E500", 67000, 145, 3800), new Car("VW", "Passat", 31000, 135, 3700) ]; // a list of sorting functions var sorters = { byWeight : function(a,b) { return (a.weight - b.weight); }, bySpeed : function(a,b) { return (a.topSpeed - b.topSpeed); }, byPrice : function(a,b) { return (a.price - b.price); }, byModelName : function(a,b) { return ((a.model < b.model) ? -1 : ((a.model > b.model) ? 1 : 0)); }, byMake : function(a,b) { return ((a.make < b.make) ? -1 : ((a.make > b.make) ? 1 : 0)); } }; function say(s) {WScript.Echo(s);} function show(title) { say ("sorted by: "+title); for (var i=0; i < autos.length; i++) { say(" " + autos[i].model); } say(" "); } autos.sort(sorters.byWeight); show("Weight"); autos.sort(sorters.byModelName); show("Name"); autos.sort(sorters.byPrice); show("Price"); 

You can also create a common sorter.

 var byProperty = function(prop) { return function(a,b) { if (typeof a[prop] == "number") { return (a[prop] - b[prop]); } else { return ((a[prop] < b[prop]) ? -1 : ((a[prop] > b[prop]) ? 1 : 0)); } }; }; autos.sort(byProperty("topSpeed")); show("Top Speed"); 
+20
Mar 17
source share

I wrote this simple function for myself:

 function sortObj(list, key) { function compare(a, b) { a = a[key]; b = b[key]; var type = (typeof(a) === 'string' || typeof(b) === 'string') ? 'string' : 'number'; var result; if (type === 'string') result = a.localeCompare(b); else result = a - b; return result; } return list.sort(compare); } 

For example, you have a list of cars:

 var cars= [{brand: 'audi', speed: 240}, {brand: 'fiat', speed: 190}]; var carsSortedByBrand = sortObj(cars, 'brand'); var carsSortedBySpeed = sortObj(cars, 'speed'); 
+13
Oct 24 '15 at 14:43
source share

Suppose we need to sort the list of objects in ascending order based on a specific property, in this example, say, we need to sort on the basis of the "name" property, below is the necessary code:

 var list_Objects = [{"name"="Bob"},{"name"="Jay"},{"name"="Abhi"}]; Console.log(list_Objects); //[{"name"="Bob"},{"name"="Jay"},{"name"="Abhi"}] list_Objects.sort(function(a,b){ return a["name"].localeCompare(b["name"]); }); Console.log(list_Objects); //[{"name"="Abhi"},{"name"="Bob"},{"name"="Jay"}] 
+5
Sep 27 '18 at 11:34
source share

A version of the Cheeso solution with reverse sorting, I also deleted ternary expressions due to lack of clarity (but this is a personal taste).

 function(prop, reverse) { return function(a, b) { if (typeof a[prop] === 'number') { return (a[prop] - b[prop]); } if (a[prop] < b[prop]) { return reverse ? 1 : -1; } if (a[prop] > b[prop]) { return reverse ? -1 : 1; } return 0; }; }; 
+2
May 08 '16 at 16:38
source share

With ES6 arrow functions, it will be like this:

 //Let say we have these cars let cars = [ { brand: 'Porsche', top_speed: 260 }, { brand: 'Benz', top_speed: 110 }, { brand: 'Fiat', top_speed: 90 }, { brand: 'Aston Martin', top_speed: 70 } ] 

Array.prototype.sort() can take a comparator function (I used the arrow notation here, but ordinary functions work the same way):

 let sortedByBrand = [...cars].sort((first, second) => first.brand > second.brand) // [ { brand: 'Aston Martin', top_speed: 70 }, // { brand: 'Benz', top_speed: 110 }, // { brand: 'Fiat', top_speed: 90 }, // { brand: 'Porsche', top_speed: 260 } ] 

The above approach copies the contents of an array of cars into a new one and sorts it alphabetically by brand. Similarly, you can pass another function:

 let sortedBySpeed =[...cars].sort((first, second) => first.top_speed > second.top_speed) //[ { brand: 'Aston Martin', top_speed: 70 }, // { brand: 'Fiat', top_speed: 90 }, // { brand: 'Benz', top_speed: 110 }, // { brand: 'Porsche', top_speed: 260 } ] 

If you do not mind changing the original cars.sort(comparatorFunction) array, everything will work out.

+1
Mar 02 '19 at 6:36
source share

Here is a short example that creates an array of objects and sorts by numbers or alphabetically:

 // Create Objects Array var arrayCarObjects = [ {brand: "Honda", topSpeed: 45}, {brand: "Ford", topSpeed: 6}, {brand: "Toyota", topSpeed: 240}, {brand: "Chevrolet", topSpeed: 120}, {brand: "Ferrari", topSpeed: 1000} ]; // Sort Objects Numerically arrayCarObjects.sort((a, b) => (a.topSpeed - b.topSpeed)); // Sort Objects Alphabetically arrayCarObjects.sort((a, b) => (a.brand > b.brand) ? 1 : -1); 
0
Jul 24 '19 at 13:28
source share



All Articles