Sort Array By Comparing 2 Elements


#1

How to sort an array of people info such as name, street, city, state, zip code

If I need to sort by city and name in ascending order so all people is sorted by city, then within city will sort by name in ascending order.

Thank you very much.


#2
let data = [{"id":1,"first_name":"Durward","last_name":"Issatt","email":"dissatt0@smh.com.au","address":"Bratislava"},
{"id":2,"first_name":"Meghan","last_name":"Corbitt","email":"mcorbitt1@alibaba.com","address":"Castro"},
{"id":3,"first_name":"Wanda","last_name":"Stockney","email":"wstockney2@e-recht24.de","address":"Caraguatatuba"},
{"id":4,"first_name":"Jeffry","last_name":"Curzon","email":"jcurzon3@so-net.ne.jp","address":"Babahoyo"},
{"id":5,"first_name":"Prince","last_name":"Guidetti","email":"pguidetti4@mapy.cz","address":"Novotroitsk"},
{"id":6,"first_name":"Angie","last_name":"Janowicz","email":"ajanowicz5@mtv.com","address":"Obonoma"},
{"id":7,"first_name":"Tristan","last_name":"Lomis","email":"tlomis6@hubpages.com","address":"San Pedro del Paraná"},
{"id":8,"first_name":"Lamond","last_name":"Baudy","email":"lbaudy7@paginegialle.it","address":"Cabatang"},
{"id":9,"first_name":"Shaylah","last_name":"Beloe","email":"sbeloe8@amazon.com","address":"Concordia"}];

data
   .sort((a,b) => a.address > b.address ? 1 : 0)
   .sort((a,b) => a.last_name > b.last_name ? 1 : 0);

This will sort the data alphabetically by address (city).

You should really use the .localeCompare but I never do.


#3

Thank you sir very much


#4

Sorry sir, I have just tried your suggestion but it does not work. I do a console.log(data); before and after sorting and both display same - unsorted. Is there another way sir?


#5

Up to you. If you send me an example of your data I’ll craft a sort for it


#6

Hang on I screwed up that logic - try this instead…

var data = [{"id":1,"first_name":"Durward","last_name":"Issatt","email":"dissatt0@smh.com.au","address":"Bratislava"},
{"id":2,"first_name":"Meghan","last_name":"Corbitt","email":"mcorbitt1@alibaba.com","address":"Bratislava"},
{"id":3,"first_name":"Wanda","last_name":"Stockney","email":"wstockney2@e-recht24.de","address":"Bratislava"},
{"id":4,"first_name":"Jeffry","last_name":"Curzon","email":"jcurzon3@so-net.ne.jp","address":"Cabatang"},
{"id":5,"first_name":"Prince","last_name":"Guidetti","email":"pguidetti4@mapy.cz","address":"Cabatang"},
{"id":6,"first_name":"Angie","last_name":"Janowicz","email":"ajanowicz5@mtv.com","address":"Cabatang"},
{"id":7,"first_name":"Tristan","last_name":"Lomis","email":"tlomis6@hubpages.com","address":"San Pedro del Paraná"},
{"id":8,"first_name":"Lamond","last_name":"Baudy","email":"lbaudy7@paginegialle.it","address":"Cabatang"},
{"id":9,"first_name":"Shaylah","last_name":"Beloe","email":"sbeloe8@amazon.com","address":"Concordia"}];

data
   .sort((a,b) => a.address.toLowerCase() > b.address.toLowerCase() || a.last_name.toLowerCase() > b.last_name.toLowerCase());

#7

Thank you for trying very hard to help me sir. However I just tried again and it still does not work.

I am using Visual Studio Code to write ionic 3 app.

Below is the current error displaying on my screen

Again. Thank you sir very much.


#8


#9

Right - apologies I just typed that straight into Chrome.which can work with booleans - whereas the convention is numeric.

Now concatenating all the properties you wish to search by.

Last time…

var data = [{"id":1,"first_name":"Durward","last_name":"Issatt","email":"dissatt0@smh.com.au","address":"Bratislava"},
{"id":4,"first_name":"Jeffry","last_name":"Curzon","email":"jcurzon3@so-net.ne.jp","address":"Cabatang"},
{"id":5,"first_name":"Prince","last_name":"Guidetti","email":"pguidetti4@mapy.cz","address":"Cabatang"},
{"id":6,"first_name":"Angie","last_name":"Janowicz","email":"ajanowicz5@mtv.com","address":"Cabatang"},
{"id":7,"first_name":"Tristan","last_name":"Lomis","email":"tlomis6@hubpages.com","address":"San Pedro del Paraná"},
{"id":8,"first_name":"Lamond","last_name":"Baudy","email":"lbaudy7@paginegialle.it","address":"Cabatang"},
{"id":2,"first_name":"Meghan","last_name":"Corbitt","email":"mcorbitt1@alibaba.com","address":"Bratislava"},
{"id":3,"first_name":"Wanda","last_name":"Stockney","email":"wstockney2@e-recht24.de","address":"Bratislava"},
{"id":9,"first_name":"Shaylah","last_name":"Beloe","email":"sbeloe8@amazon.com","address":"Concordia"}];

var result = data.sort((a,b) => `${a.address}${a.last_name}`.toLowerCase() >`${b.address}${b.last_name}`.toLowerCase() ? 1 : 0);

console.log(result);

#10

Thank you sir very much. You are very clever by concatenating all properties. This is working like a charm. Just another question. If instead of sorting by comparing 2 element of string type, I need to sort a boolean and a string such as sorting the array by isStudent DESC and Name ASC. In this scenario, isStudent will be true/false and by sorting Descending, all true will be on top of the array and non student will be on the bottom. If I concatenate properties as above, it might not work because true will be sorting to be on the bottom of the array.

var data = [{id: 1, first_name: ‘Durward’, last_name: ‘Issatt’, email: ‘dissatt0@smh.com.au’, address: ‘Bratislava’, isStudent: true},
{id: 2, first_name: ‘Meghan’, last_name: ‘Corbitt’, email: ‘mcorbitt1@alibaba.com’, address: ‘Bratislava’, isStudent: true},
{id: 3, first_name: ‘Wanda’, last_name: ‘Stockney’, email: ‘wstockney2@e-recht24.de’, address: ‘Bratislava’, isStudent: false},
{id: 4, first_name: ‘Jeffry’, last_name: ‘Curzon’, email: ‘jcurzon3@so-net.ne.jp’, address: ‘Cabatang’, isStudent: true},
{id: 5, first_name: ‘Prince’, last_name: ‘Guidetti’, email: ‘pguidetti4@mapy.cz’, address: ‘Cabatang’, isStudent: false},
{id: 6, first_name: ‘Angie’, last_name: ‘Janowicz’, email: ‘ajanowicz5@mtv.com’, address: ‘Cabatang’, isStudent: false},
{id: 7, first_name: ‘Tristan’, last_name: ‘Lomis’, email: ‘tlomis6@hubpages.com’, address: ‘San Pedro del Paraná’, isStudent: false},
{id: 8, first_name: ‘Lamond’, last_name: ‘Baudy’, email: ‘lbaudy7@paginegialle.it’, address: ‘Cabatang’, isStudent: false},
{id: 9, first_name: ‘Shaylah’, last_name: ‘Beloe’, email: ‘sbeloe8@amazon.com’, address: ‘Concordia’, isStudent: true}];


#11

How’s this?

var data = [{id: 1, first_name: 'Durward', last_name: 'Issatt', email: 'dissatt0@smh.com.au', address: 'Bratislava', isStudent: true},
{id: 2, first_name: 'Meghan', last_name: 'Corbitt', email: 'mcorbitt1@alibaba.com', address: 'Bratislava', isStudent: true},
{id: 3, first_name: 'Wanda', last_name: 'Stockney', email: 'wstockney2@e-recht24.de', address: 'Bratislava', isStudent: false},
{id: 4, first_name: 'Jeffry', last_name: 'Curzon', email: 'jcurzon3@so-net.ne.jp', address: 'Cabatang', isStudent: true},
{id: 5, first_name: 'Prince', last_name: 'Guidetti', email: 'pguidetti4@mapy.cz', address: 'Cabatang', isStudent: false},
{id: 6, first_name: 'Angie', last_name: 'Janowicz', email: 'ajanowicz5@mtv.com', address: 'Cabatang', isStudent: false},
{id: 7, first_name: 'Tristan', last_name: 'Lomis', email: 'tlomis6@hubpages.com', address: 'San Pedro del Paraná', isStudent: false},
{id: 8, first_name: 'Lamond', last_name: 'Baudy', email: 'lbaudy7@paginegialle.it', address: 'Cabatang', isStudent: false},
{id: 9, first_name: 'Shaylah', last_name: 'Beloe', email: 'sbeloe8@amazon.com', address: 'Concordia', isStudent: true}];

var result = data.sort((a,b) => `${a.isStudent ? 1 : 0}${a.first_name}${a.last_name}`.toLowerCase() > `${b.isStudent ? 1 : 0}${b.first_name}${b.last_name}`.toLowerCase() ? 1 : 0);

Will sort

  • false to true for isStudent
  • alphabetical for first_name
  • alphabetical for last_name