Is there a good reason to use `sort` with `index.return = TRUE` instead of `order`?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5















sort has the argument index.return which is by default FALSE. If you set it to TRUE you get the ordering index... basically the same as when you use order.



My question

Are there cases where it makes sense to use sort with index.return = TRUE instead of order?










share|improve this question


















  • 3





    FWIW sort calls this x[order(x, na.last = na.last, decreasing = decreasing)] for non-integer inputs.

    – hrbrmstr
    Nov 16 '18 at 12:58


















5















sort has the argument index.return which is by default FALSE. If you set it to TRUE you get the ordering index... basically the same as when you use order.



My question

Are there cases where it makes sense to use sort with index.return = TRUE instead of order?










share|improve this question


















  • 3





    FWIW sort calls this x[order(x, na.last = na.last, decreasing = decreasing)] for non-integer inputs.

    – hrbrmstr
    Nov 16 '18 at 12:58














5












5








5


1






sort has the argument index.return which is by default FALSE. If you set it to TRUE you get the ordering index... basically the same as when you use order.



My question

Are there cases where it makes sense to use sort with index.return = TRUE instead of order?










share|improve this question














sort has the argument index.return which is by default FALSE. If you set it to TRUE you get the ordering index... basically the same as when you use order.



My question

Are there cases where it makes sense to use sort with index.return = TRUE instead of order?







r sorting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 12:46









vonjdvonjd

2,13732844




2,13732844








  • 3





    FWIW sort calls this x[order(x, na.last = na.last, decreasing = decreasing)] for non-integer inputs.

    – hrbrmstr
    Nov 16 '18 at 12:58














  • 3





    FWIW sort calls this x[order(x, na.last = na.last, decreasing = decreasing)] for non-integer inputs.

    – hrbrmstr
    Nov 16 '18 at 12:58








3




3





FWIW sort calls this x[order(x, na.last = na.last, decreasing = decreasing)] for non-integer inputs.

– hrbrmstr
Nov 16 '18 at 12:58





FWIW sort calls this x[order(x, na.last = na.last, decreasing = decreasing)] for non-integer inputs.

– hrbrmstr
Nov 16 '18 at 12:58












2 Answers
2






active

oldest

votes


















6














order simply gives the indexes, instead sort gives also the values (and with index.return=T a list):



x <- runif(10, 0, 100)
order(x)
# [1] 2 7 1 9 6 5 8 10 4 3
sort(x, index.return=T)
# $`x`
# [1] 0.08140348 0.18272011 0.23575252 0.51493537 0.64281259 0.92121388 0.93759670 0.96221375 0.97646916 0.97863369
#
# $ix
# [1] 2 7 1 9 6 5 8 10 4 3


It seems that order is a little faster with big numbers (longer vector size):



x <- runif(10000000, 0, 100)

microbenchmark::microbenchmark(
sort = {sort(x, index.return=T)},
order = {x[order(x)]},
times = 100
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# sort 63.48221 67.79530 78.33724 70.74215 74.10109 173.1129 100
# order 56.46055 57.18649 60.88239 58.29462 62.13086 155.5481 100


So probably you should pick sort with index.return = TRUE only if you need a list object to be returned. I can't find an example where sort is better than the other.






share|improve this answer





















  • 1





    To clarify, by "big numbers" you mean order is faster with sorting vectors with lots of numbers, not numbers that are large in magnitude, right?

    – camille
    Nov 16 '18 at 14:04











  • Yes I meant longer vector size. I checked at it seems that even with numbers of bigger magniture is still a little faster. It doesn't change much if you use runif(10000000, 0, 100) or runif(10000000, 0, 10000).

    – RLave
    Nov 16 '18 at 14:05













  • Sometimes sort(x,method-"quick",...) is faster so you could test also with this arguments. Nice answer.

    – Csd
    Nov 16 '18 at 17:15



















1














My suggestions are based on RLave's answer.



You could use the argument method, sort(x,method="quick",index.return=TRUE), and the function might be a little faster than the default. Also if you want a faster (for large vectors) alternative method of this, you can use this function:



sort_order <- function(x){
indices <- order(x) #you can choose a method also but leave default.
list("x"=x[indices],"ix"=indices)
}


Here are some benchmarks.



microbenchmark::microbenchmark(
sort=s<-sort(x,index.return=T),
"quick sort"=sq<-sort(x,method="quick",index.return=T),
"order sort"=so<-sort_order(x),times = 10
times=10
)

Unit: seconds
expr min lq mean median uq max neval
sort 1.493714 1.662791 1.737854 1.708502 1.887993 1.960912 10
quick sort 1.366938 1.374874 1.451778 1.444342 1.480122 1.668693 10
order sort 1.181974 1.344398 1.359209 1.369108 1.424569 1.461862 10

all.equal(so,sq)
[1] TRUE
all.equal(s,so)
[1] TRUE





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',
    autoActivateHeartbeat: false,
    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%2f53338224%2fis-there-a-good-reason-to-use-sort-with-index-return-true-instead-of-orde%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    order simply gives the indexes, instead sort gives also the values (and with index.return=T a list):



    x <- runif(10, 0, 100)
    order(x)
    # [1] 2 7 1 9 6 5 8 10 4 3
    sort(x, index.return=T)
    # $`x`
    # [1] 0.08140348 0.18272011 0.23575252 0.51493537 0.64281259 0.92121388 0.93759670 0.96221375 0.97646916 0.97863369
    #
    # $ix
    # [1] 2 7 1 9 6 5 8 10 4 3


    It seems that order is a little faster with big numbers (longer vector size):



    x <- runif(10000000, 0, 100)

    microbenchmark::microbenchmark(
    sort = {sort(x, index.return=T)},
    order = {x[order(x)]},
    times = 100
    )
    # Unit: milliseconds
    # expr min lq mean median uq max neval
    # sort 63.48221 67.79530 78.33724 70.74215 74.10109 173.1129 100
    # order 56.46055 57.18649 60.88239 58.29462 62.13086 155.5481 100


    So probably you should pick sort with index.return = TRUE only if you need a list object to be returned. I can't find an example where sort is better than the other.






    share|improve this answer





















    • 1





      To clarify, by "big numbers" you mean order is faster with sorting vectors with lots of numbers, not numbers that are large in magnitude, right?

      – camille
      Nov 16 '18 at 14:04











    • Yes I meant longer vector size. I checked at it seems that even with numbers of bigger magniture is still a little faster. It doesn't change much if you use runif(10000000, 0, 100) or runif(10000000, 0, 10000).

      – RLave
      Nov 16 '18 at 14:05













    • Sometimes sort(x,method-"quick",...) is faster so you could test also with this arguments. Nice answer.

      – Csd
      Nov 16 '18 at 17:15
















    6














    order simply gives the indexes, instead sort gives also the values (and with index.return=T a list):



    x <- runif(10, 0, 100)
    order(x)
    # [1] 2 7 1 9 6 5 8 10 4 3
    sort(x, index.return=T)
    # $`x`
    # [1] 0.08140348 0.18272011 0.23575252 0.51493537 0.64281259 0.92121388 0.93759670 0.96221375 0.97646916 0.97863369
    #
    # $ix
    # [1] 2 7 1 9 6 5 8 10 4 3


    It seems that order is a little faster with big numbers (longer vector size):



    x <- runif(10000000, 0, 100)

    microbenchmark::microbenchmark(
    sort = {sort(x, index.return=T)},
    order = {x[order(x)]},
    times = 100
    )
    # Unit: milliseconds
    # expr min lq mean median uq max neval
    # sort 63.48221 67.79530 78.33724 70.74215 74.10109 173.1129 100
    # order 56.46055 57.18649 60.88239 58.29462 62.13086 155.5481 100


    So probably you should pick sort with index.return = TRUE only if you need a list object to be returned. I can't find an example where sort is better than the other.






    share|improve this answer





















    • 1





      To clarify, by "big numbers" you mean order is faster with sorting vectors with lots of numbers, not numbers that are large in magnitude, right?

      – camille
      Nov 16 '18 at 14:04











    • Yes I meant longer vector size. I checked at it seems that even with numbers of bigger magniture is still a little faster. It doesn't change much if you use runif(10000000, 0, 100) or runif(10000000, 0, 10000).

      – RLave
      Nov 16 '18 at 14:05













    • Sometimes sort(x,method-"quick",...) is faster so you could test also with this arguments. Nice answer.

      – Csd
      Nov 16 '18 at 17:15














    6












    6








    6







    order simply gives the indexes, instead sort gives also the values (and with index.return=T a list):



    x <- runif(10, 0, 100)
    order(x)
    # [1] 2 7 1 9 6 5 8 10 4 3
    sort(x, index.return=T)
    # $`x`
    # [1] 0.08140348 0.18272011 0.23575252 0.51493537 0.64281259 0.92121388 0.93759670 0.96221375 0.97646916 0.97863369
    #
    # $ix
    # [1] 2 7 1 9 6 5 8 10 4 3


    It seems that order is a little faster with big numbers (longer vector size):



    x <- runif(10000000, 0, 100)

    microbenchmark::microbenchmark(
    sort = {sort(x, index.return=T)},
    order = {x[order(x)]},
    times = 100
    )
    # Unit: milliseconds
    # expr min lq mean median uq max neval
    # sort 63.48221 67.79530 78.33724 70.74215 74.10109 173.1129 100
    # order 56.46055 57.18649 60.88239 58.29462 62.13086 155.5481 100


    So probably you should pick sort with index.return = TRUE only if you need a list object to be returned. I can't find an example where sort is better than the other.






    share|improve this answer















    order simply gives the indexes, instead sort gives also the values (and with index.return=T a list):



    x <- runif(10, 0, 100)
    order(x)
    # [1] 2 7 1 9 6 5 8 10 4 3
    sort(x, index.return=T)
    # $`x`
    # [1] 0.08140348 0.18272011 0.23575252 0.51493537 0.64281259 0.92121388 0.93759670 0.96221375 0.97646916 0.97863369
    #
    # $ix
    # [1] 2 7 1 9 6 5 8 10 4 3


    It seems that order is a little faster with big numbers (longer vector size):



    x <- runif(10000000, 0, 100)

    microbenchmark::microbenchmark(
    sort = {sort(x, index.return=T)},
    order = {x[order(x)]},
    times = 100
    )
    # Unit: milliseconds
    # expr min lq mean median uq max neval
    # sort 63.48221 67.79530 78.33724 70.74215 74.10109 173.1129 100
    # order 56.46055 57.18649 60.88239 58.29462 62.13086 155.5481 100


    So probably you should pick sort with index.return = TRUE only if you need a list object to be returned. I can't find an example where sort is better than the other.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 16 '18 at 14:10

























    answered Nov 16 '18 at 12:55









    RLaveRLave

    5,28911226




    5,28911226








    • 1





      To clarify, by "big numbers" you mean order is faster with sorting vectors with lots of numbers, not numbers that are large in magnitude, right?

      – camille
      Nov 16 '18 at 14:04











    • Yes I meant longer vector size. I checked at it seems that even with numbers of bigger magniture is still a little faster. It doesn't change much if you use runif(10000000, 0, 100) or runif(10000000, 0, 10000).

      – RLave
      Nov 16 '18 at 14:05













    • Sometimes sort(x,method-"quick",...) is faster so you could test also with this arguments. Nice answer.

      – Csd
      Nov 16 '18 at 17:15














    • 1





      To clarify, by "big numbers" you mean order is faster with sorting vectors with lots of numbers, not numbers that are large in magnitude, right?

      – camille
      Nov 16 '18 at 14:04











    • Yes I meant longer vector size. I checked at it seems that even with numbers of bigger magniture is still a little faster. It doesn't change much if you use runif(10000000, 0, 100) or runif(10000000, 0, 10000).

      – RLave
      Nov 16 '18 at 14:05













    • Sometimes sort(x,method-"quick",...) is faster so you could test also with this arguments. Nice answer.

      – Csd
      Nov 16 '18 at 17:15








    1




    1





    To clarify, by "big numbers" you mean order is faster with sorting vectors with lots of numbers, not numbers that are large in magnitude, right?

    – camille
    Nov 16 '18 at 14:04





    To clarify, by "big numbers" you mean order is faster with sorting vectors with lots of numbers, not numbers that are large in magnitude, right?

    – camille
    Nov 16 '18 at 14:04













    Yes I meant longer vector size. I checked at it seems that even with numbers of bigger magniture is still a little faster. It doesn't change much if you use runif(10000000, 0, 100) or runif(10000000, 0, 10000).

    – RLave
    Nov 16 '18 at 14:05







    Yes I meant longer vector size. I checked at it seems that even with numbers of bigger magniture is still a little faster. It doesn't change much if you use runif(10000000, 0, 100) or runif(10000000, 0, 10000).

    – RLave
    Nov 16 '18 at 14:05















    Sometimes sort(x,method-"quick",...) is faster so you could test also with this arguments. Nice answer.

    – Csd
    Nov 16 '18 at 17:15





    Sometimes sort(x,method-"quick",...) is faster so you could test also with this arguments. Nice answer.

    – Csd
    Nov 16 '18 at 17:15













    1














    My suggestions are based on RLave's answer.



    You could use the argument method, sort(x,method="quick",index.return=TRUE), and the function might be a little faster than the default. Also if you want a faster (for large vectors) alternative method of this, you can use this function:



    sort_order <- function(x){
    indices <- order(x) #you can choose a method also but leave default.
    list("x"=x[indices],"ix"=indices)
    }


    Here are some benchmarks.



    microbenchmark::microbenchmark(
    sort=s<-sort(x,index.return=T),
    "quick sort"=sq<-sort(x,method="quick",index.return=T),
    "order sort"=so<-sort_order(x),times = 10
    times=10
    )

    Unit: seconds
    expr min lq mean median uq max neval
    sort 1.493714 1.662791 1.737854 1.708502 1.887993 1.960912 10
    quick sort 1.366938 1.374874 1.451778 1.444342 1.480122 1.668693 10
    order sort 1.181974 1.344398 1.359209 1.369108 1.424569 1.461862 10

    all.equal(so,sq)
    [1] TRUE
    all.equal(s,so)
    [1] TRUE





    share|improve this answer




























      1














      My suggestions are based on RLave's answer.



      You could use the argument method, sort(x,method="quick",index.return=TRUE), and the function might be a little faster than the default. Also if you want a faster (for large vectors) alternative method of this, you can use this function:



      sort_order <- function(x){
      indices <- order(x) #you can choose a method also but leave default.
      list("x"=x[indices],"ix"=indices)
      }


      Here are some benchmarks.



      microbenchmark::microbenchmark(
      sort=s<-sort(x,index.return=T),
      "quick sort"=sq<-sort(x,method="quick",index.return=T),
      "order sort"=so<-sort_order(x),times = 10
      times=10
      )

      Unit: seconds
      expr min lq mean median uq max neval
      sort 1.493714 1.662791 1.737854 1.708502 1.887993 1.960912 10
      quick sort 1.366938 1.374874 1.451778 1.444342 1.480122 1.668693 10
      order sort 1.181974 1.344398 1.359209 1.369108 1.424569 1.461862 10

      all.equal(so,sq)
      [1] TRUE
      all.equal(s,so)
      [1] TRUE





      share|improve this answer


























        1












        1








        1







        My suggestions are based on RLave's answer.



        You could use the argument method, sort(x,method="quick",index.return=TRUE), and the function might be a little faster than the default. Also if you want a faster (for large vectors) alternative method of this, you can use this function:



        sort_order <- function(x){
        indices <- order(x) #you can choose a method also but leave default.
        list("x"=x[indices],"ix"=indices)
        }


        Here are some benchmarks.



        microbenchmark::microbenchmark(
        sort=s<-sort(x,index.return=T),
        "quick sort"=sq<-sort(x,method="quick",index.return=T),
        "order sort"=so<-sort_order(x),times = 10
        times=10
        )

        Unit: seconds
        expr min lq mean median uq max neval
        sort 1.493714 1.662791 1.737854 1.708502 1.887993 1.960912 10
        quick sort 1.366938 1.374874 1.451778 1.444342 1.480122 1.668693 10
        order sort 1.181974 1.344398 1.359209 1.369108 1.424569 1.461862 10

        all.equal(so,sq)
        [1] TRUE
        all.equal(s,so)
        [1] TRUE





        share|improve this answer













        My suggestions are based on RLave's answer.



        You could use the argument method, sort(x,method="quick",index.return=TRUE), and the function might be a little faster than the default. Also if you want a faster (for large vectors) alternative method of this, you can use this function:



        sort_order <- function(x){
        indices <- order(x) #you can choose a method also but leave default.
        list("x"=x[indices],"ix"=indices)
        }


        Here are some benchmarks.



        microbenchmark::microbenchmark(
        sort=s<-sort(x,index.return=T),
        "quick sort"=sq<-sort(x,method="quick",index.return=T),
        "order sort"=so<-sort_order(x),times = 10
        times=10
        )

        Unit: seconds
        expr min lq mean median uq max neval
        sort 1.493714 1.662791 1.737854 1.708502 1.887993 1.960912 10
        quick sort 1.366938 1.374874 1.451778 1.444342 1.480122 1.668693 10
        order sort 1.181974 1.344398 1.359209 1.369108 1.424569 1.461862 10

        all.equal(so,sq)
        [1] TRUE
        all.equal(s,so)
        [1] TRUE






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 17:33









        CsdCsd

        31819




        31819






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338224%2fis-there-a-good-reason-to-use-sort-with-index-return-true-instead-of-orde%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

            Bressuire

            Vorschmack

            Quarantine