Laravel sortBy() not working to sort a collection











up vote
0
down vote

favorite












I want to sort a collection by name column. Using sortBy() method is not working.



    $collection = AppHttpResourcesMyResource::collection($test);

return $collection->sortBy('name');









share|improve this question






















  • Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
    – Tudor
    Nov 17 at 22:05










  • It returns a collection without sorting it. Like there is no sortBy().
    – Josh
    Nov 17 at 22:09










  • Can you give an example of the data that you are trying to sort?
    – nakov
    Nov 17 at 22:11










  • The name column is simple words, they are names.
    – Josh
    Nov 17 at 22:12















up vote
0
down vote

favorite












I want to sort a collection by name column. Using sortBy() method is not working.



    $collection = AppHttpResourcesMyResource::collection($test);

return $collection->sortBy('name');









share|improve this question






















  • Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
    – Tudor
    Nov 17 at 22:05










  • It returns a collection without sorting it. Like there is no sortBy().
    – Josh
    Nov 17 at 22:09










  • Can you give an example of the data that you are trying to sort?
    – nakov
    Nov 17 at 22:11










  • The name column is simple words, they are names.
    – Josh
    Nov 17 at 22:12













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to sort a collection by name column. Using sortBy() method is not working.



    $collection = AppHttpResourcesMyResource::collection($test);

return $collection->sortBy('name');









share|improve this question













I want to sort a collection by name column. Using sortBy() method is not working.



    $collection = AppHttpResourcesMyResource::collection($test);

return $collection->sortBy('name');






laravel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 at 22:02









Josh

267




267












  • Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
    – Tudor
    Nov 17 at 22:05










  • It returns a collection without sorting it. Like there is no sortBy().
    – Josh
    Nov 17 at 22:09










  • Can you give an example of the data that you are trying to sort?
    – nakov
    Nov 17 at 22:11










  • The name column is simple words, they are names.
    – Josh
    Nov 17 at 22:12


















  • Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
    – Tudor
    Nov 17 at 22:05










  • It returns a collection without sorting it. Like there is no sortBy().
    – Josh
    Nov 17 at 22:09










  • Can you give an example of the data that you are trying to sort?
    – nakov
    Nov 17 at 22:11










  • The name column is simple words, they are names.
    – Josh
    Nov 17 at 22:12
















Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
– Tudor
Nov 17 at 22:05




Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
– Tudor
Nov 17 at 22:05












It returns a collection without sorting it. Like there is no sortBy().
– Josh
Nov 17 at 22:09




It returns a collection without sorting it. Like there is no sortBy().
– Josh
Nov 17 at 22:09












Can you give an example of the data that you are trying to sort?
– nakov
Nov 17 at 22:11




Can you give an example of the data that you are trying to sort?
– nakov
Nov 17 at 22:11












The name column is simple words, they are names.
– Josh
Nov 17 at 22:12




The name column is simple words, they are names.
– Josh
Nov 17 at 22:12












3 Answers
3






active

oldest

votes

















up vote
0
down vote













If the Laravel default sorting doesn't work for your purpose, try to do this:



$collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
return yourFunctionForSorting($element['name']);
});


Replace obviously "yourFunctionForSorting", with your sorting criteria.






share|improve this answer





















  • could you give an example of yourFunctionForSorting please, to sort alphabetically?
    – Josh
    Nov 17 at 22:19










  • @Josh Try to do this: substr($element['name'], 0, 1);
    – Tudor
    Nov 17 at 22:27












  • still not ordering
    – Josh
    Nov 17 at 22:31










  • Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
    – Tudor
    Nov 17 at 22:32










  • still doesn't work
    – Josh
    Nov 17 at 22:36


















up vote
0
down vote













The thing you want to do is:



$collection = AppHttpResourcesMyResource::collection($test);

return $collection->sortBy('name')->all();


Check how it is done in the docs.






share|improve this answer





















  • same result, unordered
    – Josh
    Nov 17 at 22:49










  • Could you try dd($collection); and post the results in your question. So we can see what is returned,
    – Thomas Van der Veen
    Nov 17 at 23:21












  • The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
    – Josh
    Nov 19 at 19:59




















up vote
0
down vote













The default sortBy for Laravel is defined as ascending:



public function sortBy($callback, $options = SORT_REGULAR, $descending = false)


If you were looking to sort descending use sortByDesc:



public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->sortBy($callback, $options, true);
}


Where the first parameter $callback can be a string or a callable, so your code could look something like this with items sorted descending:



$collection = AppHttpResourcesMyResource::collection($test);

return $collection->sortByDesc('name')->all();





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%2f53355957%2flaravel-sortby-not-working-to-sort-a-collection%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    If the Laravel default sorting doesn't work for your purpose, try to do this:



    $collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
    return yourFunctionForSorting($element['name']);
    });


    Replace obviously "yourFunctionForSorting", with your sorting criteria.






    share|improve this answer





















    • could you give an example of yourFunctionForSorting please, to sort alphabetically?
      – Josh
      Nov 17 at 22:19










    • @Josh Try to do this: substr($element['name'], 0, 1);
      – Tudor
      Nov 17 at 22:27












    • still not ordering
      – Josh
      Nov 17 at 22:31










    • Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
      – Tudor
      Nov 17 at 22:32










    • still doesn't work
      – Josh
      Nov 17 at 22:36















    up vote
    0
    down vote













    If the Laravel default sorting doesn't work for your purpose, try to do this:



    $collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
    return yourFunctionForSorting($element['name']);
    });


    Replace obviously "yourFunctionForSorting", with your sorting criteria.






    share|improve this answer





















    • could you give an example of yourFunctionForSorting please, to sort alphabetically?
      – Josh
      Nov 17 at 22:19










    • @Josh Try to do this: substr($element['name'], 0, 1);
      – Tudor
      Nov 17 at 22:27












    • still not ordering
      – Josh
      Nov 17 at 22:31










    • Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
      – Tudor
      Nov 17 at 22:32










    • still doesn't work
      – Josh
      Nov 17 at 22:36













    up vote
    0
    down vote










    up vote
    0
    down vote









    If the Laravel default sorting doesn't work for your purpose, try to do this:



    $collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
    return yourFunctionForSorting($element['name']);
    });


    Replace obviously "yourFunctionForSorting", with your sorting criteria.






    share|improve this answer












    If the Laravel default sorting doesn't work for your purpose, try to do this:



    $collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
    return yourFunctionForSorting($element['name']);
    });


    Replace obviously "yourFunctionForSorting", with your sorting criteria.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 17 at 22:15









    Tudor

    3131415




    3131415












    • could you give an example of yourFunctionForSorting please, to sort alphabetically?
      – Josh
      Nov 17 at 22:19










    • @Josh Try to do this: substr($element['name'], 0, 1);
      – Tudor
      Nov 17 at 22:27












    • still not ordering
      – Josh
      Nov 17 at 22:31










    • Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
      – Tudor
      Nov 17 at 22:32










    • still doesn't work
      – Josh
      Nov 17 at 22:36


















    • could you give an example of yourFunctionForSorting please, to sort alphabetically?
      – Josh
      Nov 17 at 22:19










    • @Josh Try to do this: substr($element['name'], 0, 1);
      – Tudor
      Nov 17 at 22:27












    • still not ordering
      – Josh
      Nov 17 at 22:31










    • Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
      – Tudor
      Nov 17 at 22:32










    • still doesn't work
      – Josh
      Nov 17 at 22:36
















    could you give an example of yourFunctionForSorting please, to sort alphabetically?
    – Josh
    Nov 17 at 22:19




    could you give an example of yourFunctionForSorting please, to sort alphabetically?
    – Josh
    Nov 17 at 22:19












    @Josh Try to do this: substr($element['name'], 0, 1);
    – Tudor
    Nov 17 at 22:27






    @Josh Try to do this: substr($element['name'], 0, 1);
    – Tudor
    Nov 17 at 22:27














    still not ordering
    – Josh
    Nov 17 at 22:31




    still not ordering
    – Josh
    Nov 17 at 22:31












    Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
    – Tudor
    Nov 17 at 22:32




    Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
    – Tudor
    Nov 17 at 22:32












    still doesn't work
    – Josh
    Nov 17 at 22:36




    still doesn't work
    – Josh
    Nov 17 at 22:36












    up vote
    0
    down vote













    The thing you want to do is:



    $collection = AppHttpResourcesMyResource::collection($test);

    return $collection->sortBy('name')->all();


    Check how it is done in the docs.






    share|improve this answer





















    • same result, unordered
      – Josh
      Nov 17 at 22:49










    • Could you try dd($collection); and post the results in your question. So we can see what is returned,
      – Thomas Van der Veen
      Nov 17 at 23:21












    • The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
      – Josh
      Nov 19 at 19:59

















    up vote
    0
    down vote













    The thing you want to do is:



    $collection = AppHttpResourcesMyResource::collection($test);

    return $collection->sortBy('name')->all();


    Check how it is done in the docs.






    share|improve this answer





















    • same result, unordered
      – Josh
      Nov 17 at 22:49










    • Could you try dd($collection); and post the results in your question. So we can see what is returned,
      – Thomas Van der Veen
      Nov 17 at 23:21












    • The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
      – Josh
      Nov 19 at 19:59















    up vote
    0
    down vote










    up vote
    0
    down vote









    The thing you want to do is:



    $collection = AppHttpResourcesMyResource::collection($test);

    return $collection->sortBy('name')->all();


    Check how it is done in the docs.






    share|improve this answer












    The thing you want to do is:



    $collection = AppHttpResourcesMyResource::collection($test);

    return $collection->sortBy('name')->all();


    Check how it is done in the docs.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 17 at 22:42









    Thomas Van der Veen

    1,4491017




    1,4491017












    • same result, unordered
      – Josh
      Nov 17 at 22:49










    • Could you try dd($collection); and post the results in your question. So we can see what is returned,
      – Thomas Van der Veen
      Nov 17 at 23:21












    • The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
      – Josh
      Nov 19 at 19:59




















    • same result, unordered
      – Josh
      Nov 17 at 22:49










    • Could you try dd($collection); and post the results in your question. So we can see what is returned,
      – Thomas Van der Veen
      Nov 17 at 23:21












    • The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
      – Josh
      Nov 19 at 19:59


















    same result, unordered
    – Josh
    Nov 17 at 22:49




    same result, unordered
    – Josh
    Nov 17 at 22:49












    Could you try dd($collection); and post the results in your question. So we can see what is returned,
    – Thomas Van der Veen
    Nov 17 at 23:21






    Could you try dd($collection); and post the results in your question. So we can see what is returned,
    – Thomas Van der Veen
    Nov 17 at 23:21














    The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
    – Josh
    Nov 19 at 19:59






    The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
    – Josh
    Nov 19 at 19:59












    up vote
    0
    down vote













    The default sortBy for Laravel is defined as ascending:



    public function sortBy($callback, $options = SORT_REGULAR, $descending = false)


    If you were looking to sort descending use sortByDesc:



    public function sortByDesc($callback, $options = SORT_REGULAR)
    {
    return $this->sortBy($callback, $options, true);
    }


    Where the first parameter $callback can be a string or a callable, so your code could look something like this with items sorted descending:



    $collection = AppHttpResourcesMyResource::collection($test);

    return $collection->sortByDesc('name')->all();





    share|improve this answer

























      up vote
      0
      down vote













      The default sortBy for Laravel is defined as ascending:



      public function sortBy($callback, $options = SORT_REGULAR, $descending = false)


      If you were looking to sort descending use sortByDesc:



      public function sortByDesc($callback, $options = SORT_REGULAR)
      {
      return $this->sortBy($callback, $options, true);
      }


      Where the first parameter $callback can be a string or a callable, so your code could look something like this with items sorted descending:



      $collection = AppHttpResourcesMyResource::collection($test);

      return $collection->sortByDesc('name')->all();





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The default sortBy for Laravel is defined as ascending:



        public function sortBy($callback, $options = SORT_REGULAR, $descending = false)


        If you were looking to sort descending use sortByDesc:



        public function sortByDesc($callback, $options = SORT_REGULAR)
        {
        return $this->sortBy($callback, $options, true);
        }


        Where the first parameter $callback can be a string or a callable, so your code could look something like this with items sorted descending:



        $collection = AppHttpResourcesMyResource::collection($test);

        return $collection->sortByDesc('name')->all();





        share|improve this answer












        The default sortBy for Laravel is defined as ascending:



        public function sortBy($callback, $options = SORT_REGULAR, $descending = false)


        If you were looking to sort descending use sortByDesc:



        public function sortByDesc($callback, $options = SORT_REGULAR)
        {
        return $this->sortBy($callback, $options, true);
        }


        Where the first parameter $callback can be a string or a callable, so your code could look something like this with items sorted descending:



        $collection = AppHttpResourcesMyResource::collection($test);

        return $collection->sortByDesc('name')->all();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 at 0:05









        adam

        456410




        456410






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355957%2flaravel-sortby-not-working-to-sort-a-collection%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

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]