How to Sort by Multiple Conditions with Different Orders

How can I sort by multiple conditions with different orders?

How about:



ordered_list = [[1, "b"], [1, "a"], [2, "a"]]
ordered_list.sort! do |a,b|
[a[0],b[1]] <=> [b[0], a[1]]
end

Sort by multiple condition in JavaScript

Your issue looks like this: "The ordinal property you want to sort by".

To be more precise, when you should sort Django and Rect.

const all_data = [{Django:10,Rect:20},{Django:20,Rect:11},{Django:1,Rect:100},{Django:100,Rect:1},{Django:0,Rect:1},{Django:0,Rect:100},{Django:0,Rect:9},{Django:0,Rect:0},{Django:0,Rect:0}];

all_data.sort((a, b) => b.Django - a.Django || b.Rect - a.Rect);
console.log(all_data);

Explain:

Firstly, sort desc by Django. Secondly, if Django is the same, then will sort desc by Rect

Sorting in SQL with multiple conditions

You’re just being asked to sort by last name in ascending order (default) and first name in descending order:

SELECT student_id, first_name, last_name, parking_place_no
FROM students
ORDER BY last_name, first_name DESC;

Sort function with multiple condition

In general if you want to sort an array of objects according to fields A, B, C (in this order) you simply do:


if (a.A < b.A) return -1;
else if (a.A > b.A) return 1;
// otherwise a.A == b.A, so let's proceed with considering B now...

if (a.B < b.B) return -1;
else if (a.B > b.B) return 1;
// otherwise a.A == b.A && a.B == b.B, so let's proceed with considering C now...
if (a.C < b.C) return -1;
else if (a.C > b.C) return 1;
return 0;

Here you can think of a.A < b.A as pseudocode for comparing that single attribute value, exactly how that is done depends on the types.

So your code should look something like

// I assume by "consider to be store open" you mean shutdown.status === false
function getShutdownStatus(a) {
return a?.shutdown?.status || false;
}
data.sort((a, b) => {
// Use ! here because true > false but it seems you want true to come first
if (!a.webOrderingEnabled < !b.webOrderingEnabled) return -1;
else if (!a.webOrderingEnabled > !b.webOrderingEnabled) return 1;
const aShutdown = getShutdownStatus(a);
const bShutdown = getShutdownStatus(b);
if (aShutdown < bShutdown) return -1;
else if (aShutdown > bShutdown) return 1;
// otherwise compare by distance
if (a.distance < b.distance) return -1;
else if (a.distance > b.distance) return 1
return 0;
});

Once you have this working you can simplify the code:

data.sort((a, b) => {
if (a.webOrderingEnabled != b.webOrderingEnabled) return b.webOrderingEnabled - a.webOrderingEnabled;
const aShutdown = getShutdownStatus(a);
const bShutdown = getShutdownStatus(b);
if (aShutdown != bShutdown) return aShutdown - bShutdown;
return a.distance - b.distance;
});

How to sort with multiple conditions in R

With dplyr:

library(dplyr)
x %>%
arrange(Age, Name)

SN Age Name
1 6 15 Chad
2 2 15 Dora
3 5 21 Bud
4 1 21 John
5 3 22 Paul
6 7 25 Anton
7 4 33 Alex

x[with(x, order(Age, Name)), ]

SN Age Name
6 6 15 Chad
2 2 15 Dora
5 5 21 Bud
1 1 21 John
3 3 22 Paul
7 7 25 Anton
4 4 33 Alex

Collections.sort for multiple conditions

You can use Java Streams. This is also used when using Collection.sort():

myList.sort(Comparator.comparing(MyObject::getAttributeX)
.thenComparing(i -> i.getSomething().getSubValue())
.thenComparing((a, b) -> a.getInt() - b.getInt()));

If you are using a lower version than Java 8 you have to implement the sort logic yourself in a Comparator or use an external library:

Collections.sort(myList, new Comparator<MyObject>() {
@Override
public int compare(MyObject a, MyObject b) {
int cmp0 = a.getAttributeX().compareTo(b.getAttributeX());
if (cmp0 != 0) {
return cmp0;
}
int cmp1 = a.getSomething().getSubValue().compareTo(b.getSomething().getSubValue());
if (cmp1 != 0) {
return cmp1;
}
return a.getInt() - b.getInt();
}
});


Related Topics



Leave a reply



Submit