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;
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
add a comment |
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
have you triedsort
?
– 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
add a comment |
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
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
php sorting
edited Mar 24 at 20:02
Qirel
14k62743
14k62743
asked Mar 24 at 19:45
Villy SkovVilly Skov
11
11
have you triedsort
?
– 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
add a comment |
have you triedsort
?
– 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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Mar 24 at 19:59
kuh-chankuh-chan
908714
908714
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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