Make Zero Appear Last in a List of Ascending Numbers

make zero appear last in a list of ascending numbers

Equivalent to the CASE statement suggestion, but shorter:

 ORDER BY epoch_date.epoch == 0, epoch_date.epoch

Make zero appear last in a sorted list of integers

You can use the fact that you can order by a boolean value. If you first order by DisplayOrder == 0 this will results to true for all zero values and false for all other values. Since true will ordered after false all zero values will be at the very end:

return foo.OrderBy(x => x.DisplayOrder == 0)
.ThenBy(x => x.DisplayOrder);

0 come last when sorting ascending

You can do it by testing for price-ordering twice:

SELECT * FROM Product P 
ORDER BY CASE WHEN @OrderBy='Date ASC' THEN Date END ASC,
CASE WHEN @OrderBy='Price ASC' THEN CASE WHEN Price = 0 THEN 1 ELSE 0 END ASC,
CASE WHEN @OrderBy='Price ASC' THEN Price END ASC,
CASE WHEN @OrderBy='Title ASC' THEN Title END ASC,
CASE WHEN @OrderBy='' THEN Match END

By the way, the implicit value of the case expression when @orderBy doesn't equal the string is null. When the sort column contains all nulls, it effectively disables sorting for that attribute.

Want to Sort an array by ascending but Zero should be at Last

The basic idea here is to treat a 0 as Infinity:

var sorted = $(".result").sort(function (ib1, ib2) {
function value(el) {
var x = parseFloat($(el).val());
return x === 0 ? Infinity : x;
}

return value(ib2) - value(ib1);
});

A simple test:

var sorted = [5, 2, 0, 9, 4].sort(function(ib1, ib2) {  function value(el) {    var x = el; // parseFloat($(el).val());    return x === 0 ? Infinity : x;  }
return value(ib2) - value(ib1);});
console.log(sorted);

Sort an array of objects in ascending order but put all zeros at the end

If a.val is 0, return 1. If b.val is 0, return -1

var array = [{val: 0}, {val: 1}, {val: 0}, {val: 3}];
array.sort((a, b) => { if (a.val === 0) return 1; //Return 1 so that b goes first if (b.val === 0) return -1; //Return -1 so that a goes first return a.val - b.val;});
console.log(array);

How to sort a ruby array in ascending order but keep zero last

So devise a comparator to do that ...

if x.total == 0
# always consider 0 "largest" and no 0 can be larger than another
# (make sure 0.0 is 0 and not a number really close to 0)
# perhaps x or y should be first for other reasons as well?
1
else
# otherwise lower to higher as normal
x.total <=> y.total
end

Or without comments:

foo.sort {|x, y| if x.total == 0 then 1 else x.total <=> y.total end}

Happy coding.

Sorting numbers in descending order but with `0`s at the start

You could sort by the delta of b and a (for descending sorting) and take Number.MAX_VALUE, for falsy values like zero.

This:

Number.MAX_VALUE - Number.MAX_VALUE

is equal to zero.

let array = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5];
array.sort((a, b) => (b || Number.MAX_VALUE) - (a || Number.MAX_VALUE));
console.log(...array);

Add column with ascending numbers starting and ending based on certain values in other column

You can use purrr:accumulate() here.
First create a logical vector with snow_depth !=0, than call accumulate with if_else.

library(purrr)
library(dplyr)

df%>%mutate(consecutive_days=accumulate(snow_depth!=0, ~if_else(.y!=0, .x+1, 0)))

snow_depth new_column consecutive_days
1 0 0 0
2 0 0 0
3 5 1 1
4 7 2 2
5 8 3 3
6 4 4 4
7 0 0 0
8 0 0 0
9 6 1 1
10 5 2 2
11 8 3 3
12 9 4 4
13 5 5 5
14 6 6 6
15 0 0 0
16 8 1 1
17 6 2 2

data

df<-data.frame(snow_depth=c(0, 0, 5, 7, 8, 4, 0, 0, 6, 5, 8, 9, 5, 6, 0, 8, 6),
new_column=c(0, 0, 1, 2, 3, 4, 0, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2))

How do I create a list with numbers between two values?

Use range. In Python 2, it returns a list directly:

>>> range(11, 17)
[11, 12, 13, 14, 15, 16]

In Python 3, range is an iterator. To convert it to a list:

>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]

Note: The second number in range(start, stop) is exclusive. So, stop = 16+1 = 17.


To increment by steps of 0.5, consider using numpy's arange() and .tolist():

>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()

[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
14.0, 14.5, 15.0, 15.5, 16.0, 16.5]

See: How do I use a decimal step value for range()?



Related Topics



Leave a reply



Submit