When attempting to organize 2 objects based on the value of first_name, one in ascending alphabetical order and the other in descending order, both end up being sorted in descending order. What could be the mistake in this code? The objective is to rearrange the objects in the array according to the first_name value.
var participants = [
{
id: "992543",
first_name: "",
last_name: "",
company: null,
notes: "",
registrationType: "",
alerts: [ ],
reg_scan: null
},
{
id: "999070",
first_name: "Tori",
last_name: "Fullard",
company: null,
notes: "",
registrationType: "Staff",
alerts: [ ],
reg_scan: null
},
{
id: "99265",
first_name: "Ronald",
last_name: "Brown",
company: null,
notes: "",
registrationType: "Dean's Guest",
alerts: [ ],
reg_scan: null
},
{
id: "992279",
first_name: "Laila",
last_name: "Shetty",
company: null,
notes: "",
registrationType: "Table Guest",
alerts: [
{
alert_id: "1",
dismissed: "0"
}
],
reg_scan: null
},
{
id: "992248",
first_name: "Paul",
last_name: "Keenan",
company: null,
notes: "",
registrationType: "Table Guest",
alerts: [ ],
reg_scan: null
}
];
var az_part = participants;
var za_part = participants;
az_part.sort(function(a, b) {
var nameA = a.first_name.toLowerCase();
var nameB = b.first_name.toLowerCase();
if (nameA > nameB) return 1;
if (nameA < nameB) return -1;
return 0;
});
za_part.sort(function(a, b) {
var nameA = a.first_name.toLowerCase();
var nameB = b.first_name.toLowerCase();
if (nameA > nameB) return -1;
if (nameA < nameB) return 1;
return 0;
});