What is the Alphabetical Inequality Test?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Mathematica can sort "b","d","a","c" into "a","b","c","d" with a simple application of Sort. What ordering function/command is being used?



It isn't <: "a" < "b" doesn't return True or False, just a < b.



I'm trying to sort a large data set that contains both strings and numeric values. An example:



data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c".



I can sort by the second element of each set:



Sort[data, #2[[2]] > #1[[2]] &] returns



4, 2, "d", 0, 3, "a", 9, 8, "b", 4, 9, "c".



But Sorting by the third element doesn't work:



Sort[data, #2[[3]] > #1[[3]] &] returns data unchanged:



9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c".



It feels like a hack, but I can sort using Ordering and OrderedQ:



data[[Ordering[data[[All, 3]]]]] returns



0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d", as does



Sort[data, OrderedQ[#1[[3]], #2[[3]]] &].



Neither feels natural. Is there a lexicographical/alphabetical "inequality" command, or is it just OrderedQ?










share|improve this question

























    up vote
    1
    down vote

    favorite












    Mathematica can sort "b","d","a","c" into "a","b","c","d" with a simple application of Sort. What ordering function/command is being used?



    It isn't <: "a" < "b" doesn't return True or False, just a < b.



    I'm trying to sort a large data set that contains both strings and numeric values. An example:



    data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c".



    I can sort by the second element of each set:



    Sort[data, #2[[2]] > #1[[2]] &] returns



    4, 2, "d", 0, 3, "a", 9, 8, "b", 4, 9, "c".



    But Sorting by the third element doesn't work:



    Sort[data, #2[[3]] > #1[[3]] &] returns data unchanged:



    9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c".



    It feels like a hack, but I can sort using Ordering and OrderedQ:



    data[[Ordering[data[[All, 3]]]]] returns



    0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d", as does



    Sort[data, OrderedQ[#1[[3]], #2[[3]]] &].



    Neither feels natural. Is there a lexicographical/alphabetical "inequality" command, or is it just OrderedQ?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Mathematica can sort "b","d","a","c" into "a","b","c","d" with a simple application of Sort. What ordering function/command is being used?



      It isn't <: "a" < "b" doesn't return True or False, just a < b.



      I'm trying to sort a large data set that contains both strings and numeric values. An example:



      data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c".



      I can sort by the second element of each set:



      Sort[data, #2[[2]] > #1[[2]] &] returns



      4, 2, "d", 0, 3, "a", 9, 8, "b", 4, 9, "c".



      But Sorting by the third element doesn't work:



      Sort[data, #2[[3]] > #1[[3]] &] returns data unchanged:



      9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c".



      It feels like a hack, but I can sort using Ordering and OrderedQ:



      data[[Ordering[data[[All, 3]]]]] returns



      0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d", as does



      Sort[data, OrderedQ[#1[[3]], #2[[3]]] &].



      Neither feels natural. Is there a lexicographical/alphabetical "inequality" command, or is it just OrderedQ?










      share|improve this question













      Mathematica can sort "b","d","a","c" into "a","b","c","d" with a simple application of Sort. What ordering function/command is being used?



      It isn't <: "a" < "b" doesn't return True or False, just a < b.



      I'm trying to sort a large data set that contains both strings and numeric values. An example:



      data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c".



      I can sort by the second element of each set:



      Sort[data, #2[[2]] > #1[[2]] &] returns



      4, 2, "d", 0, 3, "a", 9, 8, "b", 4, 9, "c".



      But Sorting by the third element doesn't work:



      Sort[data, #2[[3]] > #1[[3]] &] returns data unchanged:



      9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c".



      It feels like a hack, but I can sort using Ordering and OrderedQ:



      data[[Ordering[data[[All, 3]]]]] returns



      0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d", as does



      Sort[data, OrderedQ[#1[[3]], #2[[3]]] &].



      Neither feels natural. Is there a lexicographical/alphabetical "inequality" command, or is it just OrderedQ?







      sorting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      GregH

      565516




      565516




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You can use AlphabeticOrder:



          data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c";

          Sort[
          data,
          AlphabeticOrder[ #1[[3]], #2[[3]] ]&
          ]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"







          share|improve this answer




















          • That's it. I didn't seem to be using the right search terms online to find that command.
            – GregH
            3 hours ago

















          up vote
          2
          down vote













          AlphabeticOrder does not exist in version 10.1 which I use, but I don't believe it is necessary here. I argue that Order and OrderedQ are the more canonical (and certainly general) functions.



          Additionally, where possible you should make use of SortBy (e.g. SortBy[data, #[[3]] &]) or Ordering (as you already did) rather than Sort, as these are more efficient methods.



          An alternative to OrderedQ if you have need of Sort itself:



          Sort[data, 0 < Order[#[[3]], #2[[3]]] &]
          Sort[data, 0 > Order[#[[3]], #2[[3]]] &]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"
          4, 2, "d", 4, 9, "c", 9, 8, "b", 0, 3, "a"



          This is actually more concise than AlphabeticOrder.






          share|improve this answer
















          • 2




            In law they say "lex specialis derogat legi generali": Sometimes the more special function has its merits and is maybe is less abstract? AlphabeticOrder on the other hand is more general than Order because English is not the only language out there... ;-)
            – gwr
            59 mins ago











          • @gwr I didn't notice that AlphabeticOrder handled different languages; that's nice! I have already voted for your answer. And thanks for the Latin lesson.
            – Mr.Wizard♦
            50 mins ago










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184685%2fwhat-is-the-alphabetical-inequality-test%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          You can use AlphabeticOrder:



          data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c";

          Sort[
          data,
          AlphabeticOrder[ #1[[3]], #2[[3]] ]&
          ]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"







          share|improve this answer




















          • That's it. I didn't seem to be using the right search terms online to find that command.
            – GregH
            3 hours ago














          up vote
          4
          down vote



          accepted










          You can use AlphabeticOrder:



          data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c";

          Sort[
          data,
          AlphabeticOrder[ #1[[3]], #2[[3]] ]&
          ]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"







          share|improve this answer




















          • That's it. I didn't seem to be using the right search terms online to find that command.
            – GregH
            3 hours ago












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          You can use AlphabeticOrder:



          data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c";

          Sort[
          data,
          AlphabeticOrder[ #1[[3]], #2[[3]] ]&
          ]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"







          share|improve this answer












          You can use AlphabeticOrder:



          data = 9, 8, "b", 4, 2, "d", 0, 3, "a", 4, 9, "c";

          Sort[
          data,
          AlphabeticOrder[ #1[[3]], #2[[3]] ]&
          ]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 3 hours ago









          gwr

          6,97622457




          6,97622457











          • That's it. I didn't seem to be using the right search terms online to find that command.
            – GregH
            3 hours ago
















          • That's it. I didn't seem to be using the right search terms online to find that command.
            – GregH
            3 hours ago















          That's it. I didn't seem to be using the right search terms online to find that command.
          – GregH
          3 hours ago




          That's it. I didn't seem to be using the right search terms online to find that command.
          – GregH
          3 hours ago










          up vote
          2
          down vote













          AlphabeticOrder does not exist in version 10.1 which I use, but I don't believe it is necessary here. I argue that Order and OrderedQ are the more canonical (and certainly general) functions.



          Additionally, where possible you should make use of SortBy (e.g. SortBy[data, #[[3]] &]) or Ordering (as you already did) rather than Sort, as these are more efficient methods.



          An alternative to OrderedQ if you have need of Sort itself:



          Sort[data, 0 < Order[#[[3]], #2[[3]]] &]
          Sort[data, 0 > Order[#[[3]], #2[[3]]] &]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"
          4, 2, "d", 4, 9, "c", 9, 8, "b", 0, 3, "a"



          This is actually more concise than AlphabeticOrder.






          share|improve this answer
















          • 2




            In law they say "lex specialis derogat legi generali": Sometimes the more special function has its merits and is maybe is less abstract? AlphabeticOrder on the other hand is more general than Order because English is not the only language out there... ;-)
            – gwr
            59 mins ago











          • @gwr I didn't notice that AlphabeticOrder handled different languages; that's nice! I have already voted for your answer. And thanks for the Latin lesson.
            – Mr.Wizard♦
            50 mins ago














          up vote
          2
          down vote













          AlphabeticOrder does not exist in version 10.1 which I use, but I don't believe it is necessary here. I argue that Order and OrderedQ are the more canonical (and certainly general) functions.



          Additionally, where possible you should make use of SortBy (e.g. SortBy[data, #[[3]] &]) or Ordering (as you already did) rather than Sort, as these are more efficient methods.



          An alternative to OrderedQ if you have need of Sort itself:



          Sort[data, 0 < Order[#[[3]], #2[[3]]] &]
          Sort[data, 0 > Order[#[[3]], #2[[3]]] &]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"
          4, 2, "d", 4, 9, "c", 9, 8, "b", 0, 3, "a"



          This is actually more concise than AlphabeticOrder.






          share|improve this answer
















          • 2




            In law they say "lex specialis derogat legi generali": Sometimes the more special function has its merits and is maybe is less abstract? AlphabeticOrder on the other hand is more general than Order because English is not the only language out there... ;-)
            – gwr
            59 mins ago











          • @gwr I didn't notice that AlphabeticOrder handled different languages; that's nice! I have already voted for your answer. And thanks for the Latin lesson.
            – Mr.Wizard♦
            50 mins ago












          up vote
          2
          down vote










          up vote
          2
          down vote









          AlphabeticOrder does not exist in version 10.1 which I use, but I don't believe it is necessary here. I argue that Order and OrderedQ are the more canonical (and certainly general) functions.



          Additionally, where possible you should make use of SortBy (e.g. SortBy[data, #[[3]] &]) or Ordering (as you already did) rather than Sort, as these are more efficient methods.



          An alternative to OrderedQ if you have need of Sort itself:



          Sort[data, 0 < Order[#[[3]], #2[[3]]] &]
          Sort[data, 0 > Order[#[[3]], #2[[3]]] &]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"
          4, 2, "d", 4, 9, "c", 9, 8, "b", 0, 3, "a"



          This is actually more concise than AlphabeticOrder.






          share|improve this answer












          AlphabeticOrder does not exist in version 10.1 which I use, but I don't believe it is necessary here. I argue that Order and OrderedQ are the more canonical (and certainly general) functions.



          Additionally, where possible you should make use of SortBy (e.g. SortBy[data, #[[3]] &]) or Ordering (as you already did) rather than Sort, as these are more efficient methods.



          An alternative to OrderedQ if you have need of Sort itself:



          Sort[data, 0 < Order[#[[3]], #2[[3]]] &]
          Sort[data, 0 > Order[#[[3]], #2[[3]]] &]



          0, 3, "a", 9, 8, "b", 4, 9, "c", 4, 2, "d"
          4, 2, "d", 4, 9, "c", 9, 8, "b", 0, 3, "a"



          This is actually more concise than AlphabeticOrder.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Mr.Wizard♦

          228k294671023




          228k294671023







          • 2




            In law they say "lex specialis derogat legi generali": Sometimes the more special function has its merits and is maybe is less abstract? AlphabeticOrder on the other hand is more general than Order because English is not the only language out there... ;-)
            – gwr
            59 mins ago











          • @gwr I didn't notice that AlphabeticOrder handled different languages; that's nice! I have already voted for your answer. And thanks for the Latin lesson.
            – Mr.Wizard♦
            50 mins ago












          • 2




            In law they say "lex specialis derogat legi generali": Sometimes the more special function has its merits and is maybe is less abstract? AlphabeticOrder on the other hand is more general than Order because English is not the only language out there... ;-)
            – gwr
            59 mins ago











          • @gwr I didn't notice that AlphabeticOrder handled different languages; that's nice! I have already voted for your answer. And thanks for the Latin lesson.
            – Mr.Wizard♦
            50 mins ago







          2




          2




          In law they say "lex specialis derogat legi generali": Sometimes the more special function has its merits and is maybe is less abstract? AlphabeticOrder on the other hand is more general than Order because English is not the only language out there... ;-)
          – gwr
          59 mins ago





          In law they say "lex specialis derogat legi generali": Sometimes the more special function has its merits and is maybe is less abstract? AlphabeticOrder on the other hand is more general than Order because English is not the only language out there... ;-)
          – gwr
          59 mins ago













          @gwr I didn't notice that AlphabeticOrder handled different languages; that's nice! I have already voted for your answer. And thanks for the Latin lesson.
          – Mr.Wizard♦
          50 mins ago




          @gwr I didn't notice that AlphabeticOrder handled different languages; that's nice! I have already voted for your answer. And thanks for the Latin lesson.
          – Mr.Wizard♦
          50 mins ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184685%2fwhat-is-the-alphabetical-inequality-test%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery