sorting a while functionHow do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesHow do I sort a dictionary by value?How do I sort an NSMutableArray with custom objects in it?Sorting an array of JavaScript objects by propertySort array of objects by string property valueHow to sort a dataframe by multiple column(s)How to Sort Multi-dimensional Array by Value?Sort ArrayList of custom Objects by propertyWhy shouldn't I use mysql_* functions in PHP?

Why can my keyboard only digest 6 keypresses at a time?

Should I refuse being named as co-author of a bad quality paper?

Has there been a multiethnic Star Trek character?

Why are MBA programs closing?

Next date with distinct digits

Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?

I've been given a project I can't complete, what should I do?

Which languages would be most useful in Europe at the end of the 19th century?

Advantages of the Exponential Family: why should we study it and use it?

What are some really overused phrases in French that are common nowadays?

Can a human be transformed into a Mind Flayer?

Is using 'echo' to display attacker-controlled data on the terminal dangerous?

Printing Pascal’s triangle for n number of rows in Python

Creating an Output vs. snipping tool

The Frozen Wastes

Is it possible to have a wealthy country without a middle class?

How can I end combat quickly when the outcome is inevitable?

Why am I Seeing A Weird "Notch" on the Data Line For Some Logical 1s?

Longest bridge/tunnel that can be cycled over/through?

What is the purpose of bonds within an investment portfolio?

Increase speed altering column on large table to NON NULL

How come the nude protesters were not arrested?

If there's something that implicates the president why is there then a national security issue? (John Dowd)

Is it expected that a reader will skip parts of what you write?



sorting a while function


How do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesHow do I sort a dictionary by value?How do I sort an NSMutableArray with custom objects in it?Sorting an array of JavaScript objects by propertySort array of objects by string property valueHow to sort a dataframe by multiple column(s)How to Sort Multi-dimensional Array by Value?Sort ArrayList of custom Objects by propertyWhy shouldn't I use mysql_* functions in PHP?






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








0















I have a purchase log in my webshop admin, one costumer buy many products, all products are marked with SKU and i want to sort and group my purchase log by sku.



Is there any way to sort this and group them?



It purchase log SKU could look like this:



27318
15277
14065
6236
vild-100
N/A
blomsterkasser-40g
16694
7147
24608


and the sorted version i am looking for:



6236
7147
14065
15277
16694
24608
27318
blomsterkasser-40g
vild-100
N/A


Here is the code:



private function purchase_log_cart_items() {
while( wpsc_have_purchaselog_details() ) :
wpsc_the_purchaselog_item(); ?>
<tr>
<td><?php echo wpsc_purchaselog_details_quantity(); ?> x </td> <!-- QUANTITY! -->
<td><?php echo wpsc_purchaselog_details_name(); ?></td> <!-- NAME! -->
<td><?php echo wpsc_purchaselog_details_SKU(); ?></td> <!-- SKU! -->
<td>
<?php
echo wp.....









share|improve this question
























  • have you tried sort?

    – kuh-chan
    Mar 24 at 19:47






  • 2





    @kuh-chan That probably doesn't work since 'N/A' is the last entry in the list.

    – F.G.
    Mar 24 at 19:49

















0















I have a purchase log in my webshop admin, one costumer buy many products, all products are marked with SKU and i want to sort and group my purchase log by sku.



Is there any way to sort this and group them?



It purchase log SKU could look like this:



27318
15277
14065
6236
vild-100
N/A
blomsterkasser-40g
16694
7147
24608


and the sorted version i am looking for:



6236
7147
14065
15277
16694
24608
27318
blomsterkasser-40g
vild-100
N/A


Here is the code:



private function purchase_log_cart_items() {
while( wpsc_have_purchaselog_details() ) :
wpsc_the_purchaselog_item(); ?>
<tr>
<td><?php echo wpsc_purchaselog_details_quantity(); ?> x </td> <!-- QUANTITY! -->
<td><?php echo wpsc_purchaselog_details_name(); ?></td> <!-- NAME! -->
<td><?php echo wpsc_purchaselog_details_SKU(); ?></td> <!-- SKU! -->
<td>
<?php
echo wp.....









share|improve this question
























  • have you tried sort?

    – kuh-chan
    Mar 24 at 19:47






  • 2





    @kuh-chan That probably doesn't work since 'N/A' is the last entry in the list.

    – F.G.
    Mar 24 at 19:49













0












0








0








I have a purchase log in my webshop admin, one costumer buy many products, all products are marked with SKU and i want to sort and group my purchase log by sku.



Is there any way to sort this and group them?



It purchase log SKU could look like this:



27318
15277
14065
6236
vild-100
N/A
blomsterkasser-40g
16694
7147
24608


and the sorted version i am looking for:



6236
7147
14065
15277
16694
24608
27318
blomsterkasser-40g
vild-100
N/A


Here is the code:



private function purchase_log_cart_items() {
while( wpsc_have_purchaselog_details() ) :
wpsc_the_purchaselog_item(); ?>
<tr>
<td><?php echo wpsc_purchaselog_details_quantity(); ?> x </td> <!-- QUANTITY! -->
<td><?php echo wpsc_purchaselog_details_name(); ?></td> <!-- NAME! -->
<td><?php echo wpsc_purchaselog_details_SKU(); ?></td> <!-- SKU! -->
<td>
<?php
echo wp.....









share|improve this question
















I have a purchase log in my webshop admin, one costumer buy many products, all products are marked with SKU and i want to sort and group my purchase log by sku.



Is there any way to sort this and group them?



It purchase log SKU could look like this:



27318
15277
14065
6236
vild-100
N/A
blomsterkasser-40g
16694
7147
24608


and the sorted version i am looking for:



6236
7147
14065
15277
16694
24608
27318
blomsterkasser-40g
vild-100
N/A


Here is the code:



private function purchase_log_cart_items() {
while( wpsc_have_purchaselog_details() ) :
wpsc_the_purchaselog_item(); ?>
<tr>
<td><?php echo wpsc_purchaselog_details_quantity(); ?> x </td> <!-- QUANTITY! -->
<td><?php echo wpsc_purchaselog_details_name(); ?></td> <!-- NAME! -->
<td><?php echo wpsc_purchaselog_details_SKU(); ?></td> <!-- SKU! -->
<td>
<?php
echo wp.....






php sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 20:02









Qirel

14k62743




14k62743










asked Mar 24 at 19:45









Villy SkovVilly Skov

11




11












  • have you tried sort?

    – kuh-chan
    Mar 24 at 19:47






  • 2





    @kuh-chan That probably doesn't work since 'N/A' is the last entry in the list.

    – F.G.
    Mar 24 at 19:49

















  • have you tried sort?

    – kuh-chan
    Mar 24 at 19:47






  • 2





    @kuh-chan That probably doesn't work since 'N/A' is the last entry in the list.

    – F.G.
    Mar 24 at 19:49
















have you tried sort?

– kuh-chan
Mar 24 at 19:47





have you tried sort?

– kuh-chan
Mar 24 at 19:47




2




2





@kuh-chan That probably doesn't work since 'N/A' is the last entry in the list.

– F.G.
Mar 24 at 19:49





@kuh-chan That probably doesn't work since 'N/A' is the last entry in the list.

– F.G.
Mar 24 at 19:49












1 Answer
1






active

oldest

votes


















3














You can do it with usort and a custom sort function:



$array = [
27318,
15277,
14065,
6236,
'vild-100',
'N/A',
'blomsterkasser-40g',
16694,
7147,
24608
];

usort($array, function($left, $right)
// else 'blomsterkasser-40g' would be the first entry
$left = (string) $left;
$right = (string) $right;
// always put 'N/A' on the last position
if($left == 'N/A' && $right == 'N/A')
return 0;
else if($left == 'N/A')
return 1;
else if($right == 'N/A')
return -1;


return $left <=> $right;
);

var_dump($array);


Working code: https://3v4l.org/2K5sO






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%2f55327860%2fsorting-a-while-function%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You can do it with usort and a custom sort function:



    $array = [
    27318,
    15277,
    14065,
    6236,
    'vild-100',
    'N/A',
    'blomsterkasser-40g',
    16694,
    7147,
    24608
    ];

    usort($array, function($left, $right)
    // else 'blomsterkasser-40g' would be the first entry
    $left = (string) $left;
    $right = (string) $right;
    // always put 'N/A' on the last position
    if($left == 'N/A' && $right == 'N/A')
    return 0;
    else if($left == 'N/A')
    return 1;
    else if($right == 'N/A')
    return -1;


    return $left <=> $right;
    );

    var_dump($array);


    Working code: https://3v4l.org/2K5sO






    share|improve this answer



























      3














      You can do it with usort and a custom sort function:



      $array = [
      27318,
      15277,
      14065,
      6236,
      'vild-100',
      'N/A',
      'blomsterkasser-40g',
      16694,
      7147,
      24608
      ];

      usort($array, function($left, $right)
      // else 'blomsterkasser-40g' would be the first entry
      $left = (string) $left;
      $right = (string) $right;
      // always put 'N/A' on the last position
      if($left == 'N/A' && $right == 'N/A')
      return 0;
      else if($left == 'N/A')
      return 1;
      else if($right == 'N/A')
      return -1;


      return $left <=> $right;
      );

      var_dump($array);


      Working code: https://3v4l.org/2K5sO






      share|improve this answer

























        3












        3








        3







        You can do it with usort and a custom sort function:



        $array = [
        27318,
        15277,
        14065,
        6236,
        'vild-100',
        'N/A',
        'blomsterkasser-40g',
        16694,
        7147,
        24608
        ];

        usort($array, function($left, $right)
        // else 'blomsterkasser-40g' would be the first entry
        $left = (string) $left;
        $right = (string) $right;
        // always put 'N/A' on the last position
        if($left == 'N/A' && $right == 'N/A')
        return 0;
        else if($left == 'N/A')
        return 1;
        else if($right == 'N/A')
        return -1;


        return $left <=> $right;
        );

        var_dump($array);


        Working code: https://3v4l.org/2K5sO






        share|improve this answer













        You can do it with usort and a custom sort function:



        $array = [
        27318,
        15277,
        14065,
        6236,
        'vild-100',
        'N/A',
        'blomsterkasser-40g',
        16694,
        7147,
        24608
        ];

        usort($array, function($left, $right)
        // else 'blomsterkasser-40g' would be the first entry
        $left = (string) $left;
        $right = (string) $right;
        // always put 'N/A' on the last position
        if($left == 'N/A' && $right == 'N/A')
        return 0;
        else if($left == 'N/A')
        return 1;
        else if($right == 'N/A')
        return -1;


        return $left <=> $right;
        );

        var_dump($array);


        Working code: https://3v4l.org/2K5sO







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 19:59









        kuh-chankuh-chan

        908714




        908714





























            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%2f55327860%2fsorting-a-while-function%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript