Sort several arrays by a specific key value [duplicate]












0
















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










share|improve this question













marked as duplicate by Dominic javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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.























    0
















    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










    share|improve this question













    marked as duplicate by Dominic javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    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.





















      0












      0








      0









      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










      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 16:56









      themanwiththemasterplanthemanwiththemasterplan

      1019




      1019




      marked as duplicate by Dominic javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      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 javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      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.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          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)








          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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)








            share|improve this answer




























              0














              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)








              share|improve this answer


























                0












                0








                0







                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)








                share|improve this answer













                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)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 16:58









                Nitish NarangNitish Narang

                2,9401815




                2,9401815

















                    Popular posts from this blog

                    Futebolista

                    Jornalista

                    F# list compare