Skip to content

Instantly share code, notes, and snippets.

@dasibre
Created October 11, 2016 16:16
Show Gist options
  • Save dasibre/1453bc25eef246dd67869ba2c3b24af1 to your computer and use it in GitHub Desktop.
Save dasibre/1453bc25eef246dd67869ba2c3b24af1 to your computer and use it in GitHub Desktop.
data to be sorted and expected output
var data = [
{ lastName: 'Bishop',
firstName: 'Timothy',
gender: 'Male',
favoriteColor: 'Yellow',
dateOfBirth: '4/23/1967' },
{ lastName: 'Bonk',
firstName: 'Radek',
middleInitial: 'S',
gender: 'Male',
favoriteColor: 'Green',
dateOfBirth: '6/3/1975' },
{ lastName: 'Bouillon',
firstName: 'Francis',
middleInitial: 'G',
gender: 'Male',
favoriteColor: 'Blue',
dateOfBirth: '6/3/1975' },
{ lastName: 'Abercrombie',
firstName: 'Neil',
gender: 'Male',
favoriteColor: 'Tan',
dateOfBirth: '2/13/1943' },
{ lastName: 'Kournikova',
firstName: 'Anna',
middleInitial: 'F',
gender: 'Female',
dateOfBirth: '6/3/1975',
favoriteColor: 'Red' },
{ lastName: 'Hingis',
firstName: 'Martina',
middleInitial: 'M',
gender: 'Female',
dateOfBirth: '4/2/1979',
favoriteColor: 'Green' },
{ lastName: 'Kelly',
firstName: 'Sue',
gender: 'Female',
favoriteColor: 'Pink',
dateOfBirth: '7/12/1959' },
{ lastName: 'Seles',
firstName: 'Monica',
middleInitial: 'H',
gender: 'Female',
dateOfBirth: '12/2/1973',
favoriteColor: 'Black' },
{ lastName: 'Smith',
firstName: 'Steve',
middleInitial: 'D',
gender: 'Male',
favoriteColor: 'Red',
dateOfBirth: '3/3/1985' }
]
//output1 sort by gender(females before males) then by last name ascending
//Hingis Martina Female 4/2/1979 Green
//Kelly Sue Female 7/12/1959 Pink
//Kournikova Anna Female 6/3/1975 Red
//Seles Monica Female 12/2/1973 Black
//Abercrombie Neil Male 2/13/1943 Tan
//Bishop Timothy Male 4/23/1967 Yellow
//Bonk Radek Male 6/3/1975 Green
//Bouillon Francis Male 6/3/1975 Blue
//Smith Steve Male 3/3/1985 Red
// Output 2:sorted by birth date, ascending then by last name ascending
// Abercrombie Neil Male 2/13/1943 Tan
// Kelly Sue Female 7/12/1959 Pink
// Bishop Timothy Male 4/23/1967 Yellow
// Seles Monica Female 12/2/1973 Black
// Bonk Radek Male 6/3/1975 Green
// Bouillon Francis Male 6/3/1975 Blue
// Kournikova Anna Female 6/3/1975 Red
// Hingis Martina Female 4/2/1979 Green
// Smith Steve Male 3/3/1985 Red
// Output 3: sorted by last name, descending
// Smith Steve Male 3/3/1985 Red
// Seles Monica Female 12/2/1973 Black
// Kournikova Anna Female 6/3/1975 Red
// Kelly Sue Female 7/12/1959 Pink
// Hingis Martina Female 4/2/1979 Green
// Bouillon Francis Male 6/3/1975 Blue
// Bonk Radek Male 6/3/1975 Green
// Bishop Timothy Male 4/23/1967 Yellow
// Abercrombie Neil Male 2/13/1943 Tan
@dasibre
Copy link
Author

dasibre commented Oct 11, 2016

I have three functions to sort based on the 3 criteria, the issue i'm having is sorting based on two criteria without losing the sorted state of the first criteria when sorting based on the second.

module.exports.sortByDate = function(data) {

  return data.sort(function(x,y) {
    return new Date(x.dateOfBirth) - new Date(y.dateOfBirth);
  });
}

module.exports.sortByGender = function(data,gender) {
    return data.sort(function(x,y){
      if(+(x.gender === gender) < +(y.gender === gender)) { return 1; }
      if(+(y.gender === gender) < +(x.gender === gender)) { return -1; }
      return 0
    });
  }
function sortByLastName(data,order){
  return function(x,y) {
    if(order === 'descending') {
      return x < y;
    } else {
      return y < x;
    }
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment