Sort several arrays by a specific key value [duplicate]
This question already has an answer here:
Sort array of objects by string property value
39 answers
I have the following objects being returned from my api
country {Code: "AT", Country: "Austria", Nationality: "Austrian"}
country {Code: "BE", Country: "Belgium", Nationality: "Belgian"}
country {Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
country {Code: "DE", Country: "Germany", Nationality: "German"}
country {Code: "ES", Country: "Spain", Nationality: "Spanish"}
I need to return these in the order of the 'Country' not 'Code'.
Here is my code
this.countries = _.map(list, (item) => {
const code = item.iso;
const country = _.find(countries, (country) => country.Code == code);
//const maryPoppins = _.sortBy(country, ['country.Country']);
//console.log('maryPoppins',maryPoppins)
//returns the above objects
console.log('country', country)
return {
code: code,
name: country
}
});
I tried both sortBy and orderBy (lodash) but I think I need to merge each object into a new array and then return / sort the result.
Any help greatly appreciated
javascript lodash
marked as duplicate by Dominic
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 16:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Sort array of objects by string property value
39 answers
I have the following objects being returned from my api
country {Code: "AT", Country: "Austria", Nationality: "Austrian"}
country {Code: "BE", Country: "Belgium", Nationality: "Belgian"}
country {Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
country {Code: "DE", Country: "Germany", Nationality: "German"}
country {Code: "ES", Country: "Spain", Nationality: "Spanish"}
I need to return these in the order of the 'Country' not 'Code'.
Here is my code
this.countries = _.map(list, (item) => {
const code = item.iso;
const country = _.find(countries, (country) => country.Code == code);
//const maryPoppins = _.sortBy(country, ['country.Country']);
//console.log('maryPoppins',maryPoppins)
//returns the above objects
console.log('country', country)
return {
code: code,
name: country
}
});
I tried both sortBy and orderBy (lodash) but I think I need to merge each object into a new array and then return / sort the result.
Any help greatly appreciated
javascript lodash
marked as duplicate by Dominic
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 16:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Sort array of objects by string property value
39 answers
I have the following objects being returned from my api
country {Code: "AT", Country: "Austria", Nationality: "Austrian"}
country {Code: "BE", Country: "Belgium", Nationality: "Belgian"}
country {Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
country {Code: "DE", Country: "Germany", Nationality: "German"}
country {Code: "ES", Country: "Spain", Nationality: "Spanish"}
I need to return these in the order of the 'Country' not 'Code'.
Here is my code
this.countries = _.map(list, (item) => {
const code = item.iso;
const country = _.find(countries, (country) => country.Code == code);
//const maryPoppins = _.sortBy(country, ['country.Country']);
//console.log('maryPoppins',maryPoppins)
//returns the above objects
console.log('country', country)
return {
code: code,
name: country
}
});
I tried both sortBy and orderBy (lodash) but I think I need to merge each object into a new array and then return / sort the result.
Any help greatly appreciated
javascript lodash
This question already has an answer here:
Sort array of objects by string property value
39 answers
I have the following objects being returned from my api
country {Code: "AT", Country: "Austria", Nationality: "Austrian"}
country {Code: "BE", Country: "Belgium", Nationality: "Belgian"}
country {Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
country {Code: "DE", Country: "Germany", Nationality: "German"}
country {Code: "ES", Country: "Spain", Nationality: "Spanish"}
I need to return these in the order of the 'Country' not 'Code'.
Here is my code
this.countries = _.map(list, (item) => {
const code = item.iso;
const country = _.find(countries, (country) => country.Code == code);
//const maryPoppins = _.sortBy(country, ['country.Country']);
//console.log('maryPoppins',maryPoppins)
//returns the above objects
console.log('country', country)
return {
code: code,
name: country
}
});
I tried both sortBy and orderBy (lodash) but I think I need to merge each object into a new array and then return / sort the result.
Any help greatly appreciated
This question already has an answer here:
Sort array of objects by string property value
39 answers
javascript lodash
javascript lodash
asked Nov 26 '18 at 16:56
themanwiththemasterplanthemanwiththemasterplan
1019
1019
marked as duplicate by Dominic
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 16:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Dominic
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 16:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use localeCompare to sort strings
let arr = [{Code: "AT", Country: "Austria", Nationality: "Austrian"}
,{Code: "BE", Country: "Belgium", Nationality: "Belgian"}
,{Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
,{Code: "DE", Country: "Germany", Nationality: "German"}
,{Code: "ES", Country: "Spain", Nationality: "Spanish"}
,{Code: "IT", Country: "Italy", Nationality: "Italian"}
,{Code: "NL", Country: "Netherlands", Nationality: "Dutch"}
,{Code: "PL", Country: "Poland", Nationality: "Polish"}]
arr.sort((a,b) => a.Country.localeCompare(b.Country))
console.log(arr)add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use localeCompare to sort strings
let arr = [{Code: "AT", Country: "Austria", Nationality: "Austrian"}
,{Code: "BE", Country: "Belgium", Nationality: "Belgian"}
,{Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
,{Code: "DE", Country: "Germany", Nationality: "German"}
,{Code: "ES", Country: "Spain", Nationality: "Spanish"}
,{Code: "IT", Country: "Italy", Nationality: "Italian"}
,{Code: "NL", Country: "Netherlands", Nationality: "Dutch"}
,{Code: "PL", Country: "Poland", Nationality: "Polish"}]
arr.sort((a,b) => a.Country.localeCompare(b.Country))
console.log(arr)add a comment |
You can use localeCompare to sort strings
let arr = [{Code: "AT", Country: "Austria", Nationality: "Austrian"}
,{Code: "BE", Country: "Belgium", Nationality: "Belgian"}
,{Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
,{Code: "DE", Country: "Germany", Nationality: "German"}
,{Code: "ES", Country: "Spain", Nationality: "Spanish"}
,{Code: "IT", Country: "Italy", Nationality: "Italian"}
,{Code: "NL", Country: "Netherlands", Nationality: "Dutch"}
,{Code: "PL", Country: "Poland", Nationality: "Polish"}]
arr.sort((a,b) => a.Country.localeCompare(b.Country))
console.log(arr)add a comment |
You can use localeCompare to sort strings
let arr = [{Code: "AT", Country: "Austria", Nationality: "Austrian"}
,{Code: "BE", Country: "Belgium", Nationality: "Belgian"}
,{Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
,{Code: "DE", Country: "Germany", Nationality: "German"}
,{Code: "ES", Country: "Spain", Nationality: "Spanish"}
,{Code: "IT", Country: "Italy", Nationality: "Italian"}
,{Code: "NL", Country: "Netherlands", Nationality: "Dutch"}
,{Code: "PL", Country: "Poland", Nationality: "Polish"}]
arr.sort((a,b) => a.Country.localeCompare(b.Country))
console.log(arr)You can use localeCompare to sort strings
let arr = [{Code: "AT", Country: "Austria", Nationality: "Austrian"}
,{Code: "BE", Country: "Belgium", Nationality: "Belgian"}
,{Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
,{Code: "DE", Country: "Germany", Nationality: "German"}
,{Code: "ES", Country: "Spain", Nationality: "Spanish"}
,{Code: "IT", Country: "Italy", Nationality: "Italian"}
,{Code: "NL", Country: "Netherlands", Nationality: "Dutch"}
,{Code: "PL", Country: "Poland", Nationality: "Polish"}]
arr.sort((a,b) => a.Country.localeCompare(b.Country))
console.log(arr)let arr = [{Code: "AT", Country: "Austria", Nationality: "Austrian"}
,{Code: "BE", Country: "Belgium", Nationality: "Belgian"}
,{Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
,{Code: "DE", Country: "Germany", Nationality: "German"}
,{Code: "ES", Country: "Spain", Nationality: "Spanish"}
,{Code: "IT", Country: "Italy", Nationality: "Italian"}
,{Code: "NL", Country: "Netherlands", Nationality: "Dutch"}
,{Code: "PL", Country: "Poland", Nationality: "Polish"}]
arr.sort((a,b) => a.Country.localeCompare(b.Country))
console.log(arr)let arr = [{Code: "AT", Country: "Austria", Nationality: "Austrian"}
,{Code: "BE", Country: "Belgium", Nationality: "Belgian"}
,{Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
,{Code: "DE", Country: "Germany", Nationality: "German"}
,{Code: "ES", Country: "Spain", Nationality: "Spanish"}
,{Code: "IT", Country: "Italy", Nationality: "Italian"}
,{Code: "NL", Country: "Netherlands", Nationality: "Dutch"}
,{Code: "PL", Country: "Poland", Nationality: "Polish"}]
arr.sort((a,b) => a.Country.localeCompare(b.Country))
console.log(arr)answered Nov 26 '18 at 16:58
Nitish NarangNitish Narang
2,9401815
2,9401815
add a comment |
add a comment |