How to use lodash's _sortBy() to alphabetically sort this list with a custom requirement?









up vote
0
down vote

favorite












I have the following array of objects:



const myList = [
id: 1, title: '[A] Animal Bite - F - Not Pregnant' ,
id: 2, title: '[P] Sinus Pain - M' ,
id: 3, title: '[A] Animal Bite - F - Pregnant' ,
id: 4, title: 'Check up male' ,
id: 5, title: '[A] Animal Bite - M' ,
id: 6, title: 'Duration' ,
id: 7, title: '[P] Skin Injury - F - Not Pregnant' ,
id: 8, title: '[P] Skin Injury - M' ,
id: 9, title: 'Emergency Screening'
]


After doing:



_.sortBy(myList, 'title');


I get:



Check up male
Duration
Emergency Screening
[A] Animal Bite - F - Not Pregnant
[A] Animal Bite - F - Pregnant
[A] Animal Bite - M
[P] Sinus Pain - M
[P] Skin Injury - F - Not Pregnant
[P] Skin Injury - M


It looks good except I want the items without [A] or [P] to be at the bottom instead of the top. So like this instead:



[A] Animal Bite - F - Not Pregnant
[A] Animal Bite - F - Pregnant
[A] Animal Bite - M
[P] Sinus Pain - M
[P] Skin Injury - F - Not Pregnant
[P] Skin Injury - M
Check up male
Duration
Emergency Screening


How to achieve this?










share|improve this question

























    up vote
    0
    down vote

    favorite












    I have the following array of objects:



    const myList = [
    id: 1, title: '[A] Animal Bite - F - Not Pregnant' ,
    id: 2, title: '[P] Sinus Pain - M' ,
    id: 3, title: '[A] Animal Bite - F - Pregnant' ,
    id: 4, title: 'Check up male' ,
    id: 5, title: '[A] Animal Bite - M' ,
    id: 6, title: 'Duration' ,
    id: 7, title: '[P] Skin Injury - F - Not Pregnant' ,
    id: 8, title: '[P] Skin Injury - M' ,
    id: 9, title: 'Emergency Screening'
    ]


    After doing:



    _.sortBy(myList, 'title');


    I get:



    Check up male
    Duration
    Emergency Screening
    [A] Animal Bite - F - Not Pregnant
    [A] Animal Bite - F - Pregnant
    [A] Animal Bite - M
    [P] Sinus Pain - M
    [P] Skin Injury - F - Not Pregnant
    [P] Skin Injury - M


    It looks good except I want the items without [A] or [P] to be at the bottom instead of the top. So like this instead:



    [A] Animal Bite - F - Not Pregnant
    [A] Animal Bite - F - Pregnant
    [A] Animal Bite - M
    [P] Sinus Pain - M
    [P] Skin Injury - F - Not Pregnant
    [P] Skin Injury - M
    Check up male
    Duration
    Emergency Screening


    How to achieve this?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have the following array of objects:



      const myList = [
      id: 1, title: '[A] Animal Bite - F - Not Pregnant' ,
      id: 2, title: '[P] Sinus Pain - M' ,
      id: 3, title: '[A] Animal Bite - F - Pregnant' ,
      id: 4, title: 'Check up male' ,
      id: 5, title: '[A] Animal Bite - M' ,
      id: 6, title: 'Duration' ,
      id: 7, title: '[P] Skin Injury - F - Not Pregnant' ,
      id: 8, title: '[P] Skin Injury - M' ,
      id: 9, title: 'Emergency Screening'
      ]


      After doing:



      _.sortBy(myList, 'title');


      I get:



      Check up male
      Duration
      Emergency Screening
      [A] Animal Bite - F - Not Pregnant
      [A] Animal Bite - F - Pregnant
      [A] Animal Bite - M
      [P] Sinus Pain - M
      [P] Skin Injury - F - Not Pregnant
      [P] Skin Injury - M


      It looks good except I want the items without [A] or [P] to be at the bottom instead of the top. So like this instead:



      [A] Animal Bite - F - Not Pregnant
      [A] Animal Bite - F - Pregnant
      [A] Animal Bite - M
      [P] Sinus Pain - M
      [P] Skin Injury - F - Not Pregnant
      [P] Skin Injury - M
      Check up male
      Duration
      Emergency Screening


      How to achieve this?










      share|improve this question













      I have the following array of objects:



      const myList = [
      id: 1, title: '[A] Animal Bite - F - Not Pregnant' ,
      id: 2, title: '[P] Sinus Pain - M' ,
      id: 3, title: '[A] Animal Bite - F - Pregnant' ,
      id: 4, title: 'Check up male' ,
      id: 5, title: '[A] Animal Bite - M' ,
      id: 6, title: 'Duration' ,
      id: 7, title: '[P] Skin Injury - F - Not Pregnant' ,
      id: 8, title: '[P] Skin Injury - M' ,
      id: 9, title: 'Emergency Screening'
      ]


      After doing:



      _.sortBy(myList, 'title');


      I get:



      Check up male
      Duration
      Emergency Screening
      [A] Animal Bite - F - Not Pregnant
      [A] Animal Bite - F - Pregnant
      [A] Animal Bite - M
      [P] Sinus Pain - M
      [P] Skin Injury - F - Not Pregnant
      [P] Skin Injury - M


      It looks good except I want the items without [A] or [P] to be at the bottom instead of the top. So like this instead:



      [A] Animal Bite - F - Not Pregnant
      [A] Animal Bite - F - Pregnant
      [A] Animal Bite - M
      [P] Sinus Pain - M
      [P] Skin Injury - F - Not Pregnant
      [P] Skin Injury - M
      Check up male
      Duration
      Emergency Screening


      How to achieve this?







      javascript underscore.js lodash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 21:36









      TK123

      10.4k36113156




      10.4k36113156






















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Use localeCompare with numeric: false option in the sort function:






          const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

          const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, numeric: false))

          console.log(r)





          Another way you can also get the same result is via the caseFirst: lower' option parameter as noted (and explained) by @xehpuk asnwer:






          const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

          const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, caseFirst: 'lower'))

          console.log(r)





          You also do not need lodash for this if ES6 is an option.






          share|improve this answer





























            up vote
            1
            down vote













            lodash's sortBy may take list of comparators. So you can just declare "words don't start from square bracket go later" and inside the "group" sort by title



            _.sortBy(myList, [
            item => !item.title.startsWith("["),
            'title'
            ]);


            And with orderBy you even can specify ordering in more readable(and flexible) way:



            _.orderBy(myList, [
            item => item.title.startsWith("["),
            'title'
            ], ['desc', 'asc']);


            [UPD] with startsWith mentioned by @Ele it looks even better






            share|improve this answer





























              up vote
              1
              down vote













              If you really want to use lodash, you can compare the titles in lower-case:



              _.sortBy(myList, item => item.title.toLowerCase());


              This works because the code unit of the lower-case characters (97 - 122) is greater than the one of [ (91). This would also have the benefit of comparing the titles case-insensitively.






              share|improve this answer



























                up vote
                1
                down vote













                This is an alternative using the function Array.prototype.sort()



                Assuming the there is max one [ in the string.






                const myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                myList.sort((a, b) =>
                if (a.title.startsWith("[") && b.title.startsWith("["))
                return a.title.substring(1).localeCompare(b.title.substring(1));


                return a.title.localeCompare(b.title);
                );

                console.log(myList);

                .as-console-wrapper max-height: 100% !important; top: 0; 








                share|improve this answer






















                • Your comparator doesn't sort the titles not starting with [. It just moves them to the end.
                  – xehpuk
                  Nov 9 at 22:12

















                up vote
                0
                down vote













                You could move the brackets parts to top by checking the strings, then takes the local compare result.






                const
                startsWithBrackets = s => /^[.+]/.test(s),
                myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                myList.sort(( title: a , title: b ) =>
                startsWithBrackets(b) - startsWithBrackets(a) || a.localeCompare(b));

                console.log(myList);

                .as-console-wrapper max-height: 100% !important; top: 0; 








                share|improve this answer




















                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233552%2fhow-to-use-lodashs-sortby-to-alphabetically-sort-this-list-with-a-custom-req%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  1
                  down vote



                  accepted










                  Use localeCompare with numeric: false option in the sort function:






                  const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                  const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, numeric: false))

                  console.log(r)





                  Another way you can also get the same result is via the caseFirst: lower' option parameter as noted (and explained) by @xehpuk asnwer:






                  const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                  const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, caseFirst: 'lower'))

                  console.log(r)





                  You also do not need lodash for this if ES6 is an option.






                  share|improve this answer


























                    up vote
                    1
                    down vote



                    accepted










                    Use localeCompare with numeric: false option in the sort function:






                    const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                    const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, numeric: false))

                    console.log(r)





                    Another way you can also get the same result is via the caseFirst: lower' option parameter as noted (and explained) by @xehpuk asnwer:






                    const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                    const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, caseFirst: 'lower'))

                    console.log(r)





                    You also do not need lodash for this if ES6 is an option.






                    share|improve this answer
























                      up vote
                      1
                      down vote



                      accepted







                      up vote
                      1
                      down vote



                      accepted






                      Use localeCompare with numeric: false option in the sort function:






                      const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                      const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, numeric: false))

                      console.log(r)





                      Another way you can also get the same result is via the caseFirst: lower' option parameter as noted (and explained) by @xehpuk asnwer:






                      const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                      const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, caseFirst: 'lower'))

                      console.log(r)





                      You also do not need lodash for this if ES6 is an option.






                      share|improve this answer














                      Use localeCompare with numeric: false option in the sort function:






                      const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                      const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, numeric: false))

                      console.log(r)





                      Another way you can also get the same result is via the caseFirst: lower' option parameter as noted (and explained) by @xehpuk asnwer:






                      const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                      const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, caseFirst: 'lower'))

                      console.log(r)





                      You also do not need lodash for this if ES6 is an option.






                      const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                      const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, numeric: false))

                      console.log(r)





                      const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                      const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, numeric: false))

                      console.log(r)





                      const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                      const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, caseFirst: 'lower'))

                      console.log(r)





                      const list = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ] 

                      const r = list.sort((a,b) => a.title.localeCompare(b.title, 0, caseFirst: 'lower'))

                      console.log(r)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 15 at 1:24

























                      answered Nov 9 at 21:56









                      Akrion

                      7,19111222




                      7,19111222






















                          up vote
                          1
                          down vote













                          lodash's sortBy may take list of comparators. So you can just declare "words don't start from square bracket go later" and inside the "group" sort by title



                          _.sortBy(myList, [
                          item => !item.title.startsWith("["),
                          'title'
                          ]);


                          And with orderBy you even can specify ordering in more readable(and flexible) way:



                          _.orderBy(myList, [
                          item => item.title.startsWith("["),
                          'title'
                          ], ['desc', 'asc']);


                          [UPD] with startsWith mentioned by @Ele it looks even better






                          share|improve this answer


























                            up vote
                            1
                            down vote













                            lodash's sortBy may take list of comparators. So you can just declare "words don't start from square bracket go later" and inside the "group" sort by title



                            _.sortBy(myList, [
                            item => !item.title.startsWith("["),
                            'title'
                            ]);


                            And with orderBy you even can specify ordering in more readable(and flexible) way:



                            _.orderBy(myList, [
                            item => item.title.startsWith("["),
                            'title'
                            ], ['desc', 'asc']);


                            [UPD] with startsWith mentioned by @Ele it looks even better






                            share|improve this answer
























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              lodash's sortBy may take list of comparators. So you can just declare "words don't start from square bracket go later" and inside the "group" sort by title



                              _.sortBy(myList, [
                              item => !item.title.startsWith("["),
                              'title'
                              ]);


                              And with orderBy you even can specify ordering in more readable(and flexible) way:



                              _.orderBy(myList, [
                              item => item.title.startsWith("["),
                              'title'
                              ], ['desc', 'asc']);


                              [UPD] with startsWith mentioned by @Ele it looks even better






                              share|improve this answer














                              lodash's sortBy may take list of comparators. So you can just declare "words don't start from square bracket go later" and inside the "group" sort by title



                              _.sortBy(myList, [
                              item => !item.title.startsWith("["),
                              'title'
                              ]);


                              And with orderBy you even can specify ordering in more readable(and flexible) way:



                              _.orderBy(myList, [
                              item => item.title.startsWith("["),
                              'title'
                              ], ['desc', 'asc']);


                              [UPD] with startsWith mentioned by @Ele it looks even better







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 9 at 22:05

























                              answered Nov 9 at 21:59









                              skyboyer

                              2,93811028




                              2,93811028




















                                  up vote
                                  1
                                  down vote













                                  If you really want to use lodash, you can compare the titles in lower-case:



                                  _.sortBy(myList, item => item.title.toLowerCase());


                                  This works because the code unit of the lower-case characters (97 - 122) is greater than the one of [ (91). This would also have the benefit of comparing the titles case-insensitively.






                                  share|improve this answer
























                                    up vote
                                    1
                                    down vote













                                    If you really want to use lodash, you can compare the titles in lower-case:



                                    _.sortBy(myList, item => item.title.toLowerCase());


                                    This works because the code unit of the lower-case characters (97 - 122) is greater than the one of [ (91). This would also have the benefit of comparing the titles case-insensitively.






                                    share|improve this answer






















                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      If you really want to use lodash, you can compare the titles in lower-case:



                                      _.sortBy(myList, item => item.title.toLowerCase());


                                      This works because the code unit of the lower-case characters (97 - 122) is greater than the one of [ (91). This would also have the benefit of comparing the titles case-insensitively.






                                      share|improve this answer












                                      If you really want to use lodash, you can compare the titles in lower-case:



                                      _.sortBy(myList, item => item.title.toLowerCase());


                                      This works because the code unit of the lower-case characters (97 - 122) is greater than the one of [ (91). This would also have the benefit of comparing the titles case-insensitively.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 9 at 22:07









                                      xehpuk

                                      4,1282335




                                      4,1282335




















                                          up vote
                                          1
                                          down vote













                                          This is an alternative using the function Array.prototype.sort()



                                          Assuming the there is max one [ in the string.






                                          const myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                          myList.sort((a, b) =>
                                          if (a.title.startsWith("[") && b.title.startsWith("["))
                                          return a.title.substring(1).localeCompare(b.title.substring(1));


                                          return a.title.localeCompare(b.title);
                                          );

                                          console.log(myList);

                                          .as-console-wrapper max-height: 100% !important; top: 0; 








                                          share|improve this answer






















                                          • Your comparator doesn't sort the titles not starting with [. It just moves them to the end.
                                            – xehpuk
                                            Nov 9 at 22:12














                                          up vote
                                          1
                                          down vote













                                          This is an alternative using the function Array.prototype.sort()



                                          Assuming the there is max one [ in the string.






                                          const myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                          myList.sort((a, b) =>
                                          if (a.title.startsWith("[") && b.title.startsWith("["))
                                          return a.title.substring(1).localeCompare(b.title.substring(1));


                                          return a.title.localeCompare(b.title);
                                          );

                                          console.log(myList);

                                          .as-console-wrapper max-height: 100% !important; top: 0; 








                                          share|improve this answer






















                                          • Your comparator doesn't sort the titles not starting with [. It just moves them to the end.
                                            – xehpuk
                                            Nov 9 at 22:12












                                          up vote
                                          1
                                          down vote










                                          up vote
                                          1
                                          down vote









                                          This is an alternative using the function Array.prototype.sort()



                                          Assuming the there is max one [ in the string.






                                          const myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                          myList.sort((a, b) =>
                                          if (a.title.startsWith("[") && b.title.startsWith("["))
                                          return a.title.substring(1).localeCompare(b.title.substring(1));


                                          return a.title.localeCompare(b.title);
                                          );

                                          console.log(myList);

                                          .as-console-wrapper max-height: 100% !important; top: 0; 








                                          share|improve this answer














                                          This is an alternative using the function Array.prototype.sort()



                                          Assuming the there is max one [ in the string.






                                          const myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                          myList.sort((a, b) =>
                                          if (a.title.startsWith("[") && b.title.startsWith("["))
                                          return a.title.substring(1).localeCompare(b.title.substring(1));


                                          return a.title.localeCompare(b.title);
                                          );

                                          console.log(myList);

                                          .as-console-wrapper max-height: 100% !important; top: 0; 








                                          const myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                          myList.sort((a, b) =>
                                          if (a.title.startsWith("[") && b.title.startsWith("["))
                                          return a.title.substring(1).localeCompare(b.title.substring(1));


                                          return a.title.localeCompare(b.title);
                                          );

                                          console.log(myList);

                                          .as-console-wrapper max-height: 100% !important; top: 0; 





                                          const myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                          myList.sort((a, b) =>
                                          if (a.title.startsWith("[") && b.title.startsWith("["))
                                          return a.title.substring(1).localeCompare(b.title.substring(1));


                                          return a.title.localeCompare(b.title);
                                          );

                                          console.log(myList);

                                          .as-console-wrapper max-height: 100% !important; top: 0; 






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Nov 9 at 22:20

























                                          answered Nov 9 at 21:42









                                          Ele

                                          22.3k42044




                                          22.3k42044











                                          • Your comparator doesn't sort the titles not starting with [. It just moves them to the end.
                                            – xehpuk
                                            Nov 9 at 22:12
















                                          • Your comparator doesn't sort the titles not starting with [. It just moves them to the end.
                                            – xehpuk
                                            Nov 9 at 22:12















                                          Your comparator doesn't sort the titles not starting with [. It just moves them to the end.
                                          – xehpuk
                                          Nov 9 at 22:12




                                          Your comparator doesn't sort the titles not starting with [. It just moves them to the end.
                                          – xehpuk
                                          Nov 9 at 22:12










                                          up vote
                                          0
                                          down vote













                                          You could move the brackets parts to top by checking the strings, then takes the local compare result.






                                          const
                                          startsWithBrackets = s => /^[.+]/.test(s),
                                          myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                          myList.sort(( title: a , title: b ) =>
                                          startsWithBrackets(b) - startsWithBrackets(a) || a.localeCompare(b));

                                          console.log(myList);

                                          .as-console-wrapper max-height: 100% !important; top: 0; 








                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            You could move the brackets parts to top by checking the strings, then takes the local compare result.






                                            const
                                            startsWithBrackets = s => /^[.+]/.test(s),
                                            myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                            myList.sort(( title: a , title: b ) =>
                                            startsWithBrackets(b) - startsWithBrackets(a) || a.localeCompare(b));

                                            console.log(myList);

                                            .as-console-wrapper max-height: 100% !important; top: 0; 








                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              You could move the brackets parts to top by checking the strings, then takes the local compare result.






                                              const
                                              startsWithBrackets = s => /^[.+]/.test(s),
                                              myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                              myList.sort(( title: a , title: b ) =>
                                              startsWithBrackets(b) - startsWithBrackets(a) || a.localeCompare(b));

                                              console.log(myList);

                                              .as-console-wrapper max-height: 100% !important; top: 0; 








                                              share|improve this answer












                                              You could move the brackets parts to top by checking the strings, then takes the local compare result.






                                              const
                                              startsWithBrackets = s => /^[.+]/.test(s),
                                              myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                              myList.sort(( title: a , title: b ) =>
                                              startsWithBrackets(b) - startsWithBrackets(a) || a.localeCompare(b));

                                              console.log(myList);

                                              .as-console-wrapper max-height: 100% !important; top: 0; 








                                              const
                                              startsWithBrackets = s => /^[.+]/.test(s),
                                              myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                              myList.sort(( title: a , title: b ) =>
                                              startsWithBrackets(b) - startsWithBrackets(a) || a.localeCompare(b));

                                              console.log(myList);

                                              .as-console-wrapper max-height: 100% !important; top: 0; 





                                              const
                                              startsWithBrackets = s => /^[.+]/.test(s),
                                              myList = [ id: 1, title: '[A] Animal Bite - F - Not Pregnant' , id: 2, title: '[P] Sinus Pain - M' , id: 3, title: '[A] Animal Bite - F - Pregnant' , id: 4, title: 'Check up male' , id: 5, title: '[A] Animal Bite - M' , id: 6, title: 'Duration' , id: 7, title: '[P] Skin Injury - F - Not Pregnant' , id: 8, title: '[P] Skin Injury - M' , id: 9, title: 'Emergency Screening' ];

                                              myList.sort(( title: a , title: b ) =>
                                              startsWithBrackets(b) - startsWithBrackets(a) || a.localeCompare(b));

                                              console.log(myList);

                                              .as-console-wrapper max-height: 100% !important; top: 0; 






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 9 at 22:15









                                              Nina Scholz

                                              170k1382146




                                              170k1382146



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233552%2fhow-to-use-lodashs-sortby-to-alphabetically-sort-this-list-with-a-custom-req%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Use pre created SQLite database for Android project in kotlin

                                                  Darth Vader #20

                                                  Ondo