I have an array and I am sorting it but I need to sort everything except one element of my array.
My array is:
var Comparison = [
{key: \"None\", val
Alternatively you can filter out the nones and sort the other elements. Then concatenate them back to each other at the end.
const comparison = [{key: "None", value: "None"}, {key: "Geographical Area", value: "Geographical_Area"}, {key: "Forests", value: "Forests"}, {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}, {key: "Land put to Non agricultural use", value: "Land_put_to_Non_agricultural_use"}, {key: "Land Area", value: "Land_Area"}, {key: "Water Area", value: "Water_Area"}, {key: "Culturable Waste", value: "Culturable_Waste"}, {key: "Permanent Pastures", value: "Permanent_Pastures"}, {key: "Land under Tree Crops", value: "Land_under_Tree_Crops"}, {key: "Fallow Land excl Current Fallow", value: "Fallow_Land_excl_Current_Fallow"}, {key: "Current Fallow", value: "Current_Fallow"}, {key: "Total Unculturable Land", value: "Total_Unculturable_Land"}, {key: "Net Sown Area", value: "Net_Sown_Area"}, {key: "Gross Sown Area", value: "Gross_Sown_Area"}, {key: "Cropping Intensity", value: "Cropping_Intensity"}];
const result = comparison
.filter(e => e.key === 'None')
.concat(
comparison.filter(e => e.key !== 'None')
.sort((a, b) => a.key.localeCompare(b.key))
);
console.log(result);
Explanation:
const comparison = [{key: "None", value: "None"}, {key: "Geographical Area", value: "Geographical_Area"}, {key: "Forests", value: "Forests"}, {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}, {key: "Land put to Non agricultural use", value: "Land_put_to_Non_agricultural_use"}, {key: "Land Area", value: "Land_Area"}, {key: "Water Area", value: "Water_Area"}, {key: "Culturable Waste", value: "Culturable_Waste"}, {key: "Permanent Pastures", value: "Permanent_Pastures"}, {key: "Land under Tree Crops", value: "Land_under_Tree_Crops"}, {key: "Fallow Land excl Current Fallow", value: "Fallow_Land_excl_Current_Fallow"}, {key: "Current Fallow", value: "Current_Fallow"}, {key: "Total Unculturable Land", value: "Total_Unculturable_Land"}, {key: "Net Sown Area", value: "Net_Sown_Area"}, {key: "Gross Sown Area", value: "Gross_Sown_Area"}, {key: "Cropping Intensity", value: "Cropping_Intensity"}];
// fetch all elements with the key 'None'
const nones = comparison.filter(e => e.key === 'None');
// fetch all elements with the key not 'None'
const others = comparison.filter(e => e.key !== 'None')
// sort the elements in the array by key
.sort((a, b) => a.key.localeCompare(b.key));
// concatenate the 2 arrays together
const result = nones.concat(others);
console.log(result);
A bit of credit to Pac0s answer. After writing my solution I saw that I basically made a working version of his explanation. I'm too late to add my example to his answer because this is currently the most upvoted of the two.
For larger arrays using filter()
two times on the same array with the opposite predicate (check callback) might be a bit inefficient. You could opt to introduce a helper function like partition()
to help reduce the amount of iteration that has to be done.
function partition(forEachable, callback) {
const partitions = { "true": [], "false": [] };
forEachable.forEach((...args) => partitions[!!callback(...args)].push(args[0]));
return [partitions[true], partitions[false]];
}
let comparison = [{key: "None", value: "None"}, {key: "Geographical Area", value: "Geographical_Area"}, {key: "Forests", value: "Forests"}, {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}, {key: "Land put to Non agricultural use", value: "Land_put_to_Non_agricultural_use"}, {key: "Land Area", value: "Land_Area"}, {key: "Water Area", value: "Water_Area"}, {key: "Culturable Waste", value: "Culturable_Waste"}, {key: "Permanent Pastures", value: "Permanent_Pastures"}, {key: "Land under Tree Crops", value: "Land_under_Tree_Crops"}, {key: "Fallow Land excl Current Fallow", value: "Fallow_Land_excl_Current_Fallow"}, {key: "Current Fallow", value: "Current_Fallow"}, {key: "Total Unculturable Land", value: "Total_Unculturable_Land"}, {key: "Net Sown Area", value: "Net_Sown_Area"}, {key: "Gross Sown Area", value: "Gross_Sown_Area"}, {key: "Cropping Intensity", value: "Cropping_Intensity"}];
const [nones, others] = partition(comparison, e => e.key === "None");
others.sort((a, b) => a.key.localeCompare(b.key));
const result = nones.concat(others);
console.log(result);