Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

71%
+3 −0
Challenges Generalized Sort

Challenge We all know and love the generic sort function, right? However, it only sorts based off one criterion - what if we want more? That's where you come in. Your task is to sort an array bas...

6 answers  ·  posted 2y ago by Moshi‭  ·  last activity 2y ago by radarek‭

Question code-golf sorting
#2: Post edited by user avatar Moshi‭ · 2021-09-01T23:00:39Z (over 2 years ago)
  • # Challenge
  • We all know and love the generic sort function, right? However, it only sorts based off one criterion - what if we want more? That's where you come in.
  • Your task is to sort an array based off an arbitrary number of comparison functions `c1`, `c2`, `c3`, etc.; first sort by comparing using `c1`, if there is a tie then sort by `c2`, if there is still a tie sort by `c3`, etc. Output order is undefined if none of the comparison functions breaks the tie.
  • ## Example
  • Here is an example in JavaScript:
  • ```javascript
  • function sort(array, ...comparators) {
  • return array.sort((a, b) =>
  • comparators.map(c => c(a, b)) // Apply comparisons
  • .find(r => r != 0) // Find the first non-zero comparison
  • );
  • }
  • ```
  • [Try it online!](https://tio.run/##dZJNb8IwDIbP9Fd4XGg1CB/XiUmAtAPStAOHSWMcTAhrUEkqJwyxab@9c1sYtLBKTdU3z2s7sTf4iU6STn3H2JXKsvXOSK@tAWfJh0iEhzYIIaTdpkjoLbkIvoMGKb8jAwUgCjbENiwjGD4GjcYFLbaYhpJlkCURQbcLozRNDlBi2lnj2ATnR6y1WYWUuwjuhtCLIHc9sQo@VrDW5DwYazpfiuxFnKARPQQ/meSIHrziZQjzIA/ZnMRMeG0QpjY2zDbb5cbMqz3S6j/5NdZe1cWX1pg0XrGzrfbxSRzrJKkqeYZ6milulatiM0z4cmrcufxndVDkKnpNO5VTVYt6aoW/oYyRDtX8Zcgx2f0VV7OPlktVM5/Iau7i5JWI5SFvWm@d/NiGYFHMk@FLy4cj/wqXJtqHLWhF3PugbL3sc@P/JhJw3l@IxEpM1KSYFRUuWWL@iA9qeO8a7y1O4W2iePcjLMY@H7I25@N3EF0Vxy64B66N1@Kfk4qN1SZsvZu84OwX "JavaScript (Node.js) – Try It Online")
  • ## Test Cases
  • Here is a test case, sorting first by last name then by first name.[^1]
  • ```
  • Christina Johnson
  • Steward Johnson
  • Steward White
  • Steward O'Brian
  • Steward Smith
  • Bill Smith
  • John Johnson
  • James Smith
  • Sally Johnson
  • Christina Meyers
  • Chris Meyers
  • Steward Meyers
  • Bill O'Brian
  • Zachary Smith
  • Chris Brown
  • Zachary O'Brian
  • Abbey Smith
  • Zachary Meyers
  • John Brown
  • Sally Smith
  • Zachary Johnson
  • Chris White
  • =>
  • Chris Brown
  • John Brown
  • Christina Johnson
  • John Johnson
  • Sally Johnson
  • Steward Johnson
  • Zachary Johnson
  • Chris Meyers
  • Christina Meyers
  • Steward Meyers
  • Zachary Meyers
  • Bill O'Brian
  • Steward O'Brian
  • Zachary O'Brian
  • Abbey Smith
  • Bill Smith
  • James Smith
  • Sally Smith
  • Steward Smith
  • Zachary Smith
  • Chris White
  • Steward White
  • ```
  • This is <a class="badge is-tag" href="https://codegolf.codidact.com/categories/49/tags/4274">code-golf</a>, so the entry with the lowest bytes wins!
  • [^1]: Incidentally, I generated this data with a short script
  • # Challenge
  • We all know and love the generic sort function, right? However, it only sorts based off one criterion - what if we want more? That's where you come in.
  • Your task is to sort an array based off an arbitrary number of comparison functions `c1`, `c2`, `c3`, etc.; first sort by comparing using `c1`, if there is a tie then sort by `c2`, if there is still a tie sort by `c3`, etc. If there are a set of items that are still tied after all comparisons are made, the order of them with respect to each other can be implemented however you like (including an unstable sort). However, their order with respect to others must be correct. For instance, say we are sorting only using the `x` property of a list of objects, and we are given an input of
  • ```
  • [ { x: 1, y: 2 },
  • { x: 2, y: 1 },
  • { x: 1, y: 1 } ]
  • ```
  • You may output either of
  • ```
  • [ { x: 1, y: 2 },
  • { x: 1, y: 1 },
  • { x: 2, y: 1 } ]
  • ```
  • or
  • ```
  • [ { x: 1, y: 1 },
  • { x: 1, y: 2 },
  • { x: 2, y: 1 } ]
  • ```
  • ## Input
  • You may take input in any format that makes sense, e.g. as an array of functions in either precedence order and the array to sort.
  • ## Example
  • Here is an example in JavaScript:
  • ```javascript
  • function sort(array, ...comparators) {
  • return array.sort((a, b) =>
  • comparators.map(c => c(a, b)) // Apply comparisons
  • .find(r => r != 0) // Find the first non-zero comparison
  • );
  • }
  • ```
  • [Try it online!](https://tio.run/##dZJNb8IwDIbP9Fd4XGg1CB/XiUmAtAPStAOHSWMcTAhrUEkqJwyxab@9c1sYtLBKTdU3z2s7sTf4iU6STn3H2JXKsvXOSK@tAWfJh0iEhzYIIaTdpkjoLbkIvoMGKb8jAwUgCjbENiwjGD4GjcYFLbaYhpJlkCURQbcLozRNDlBi2lnj2ATnR6y1WYWUuwjuhtCLIHc9sQo@VrDW5DwYazpfiuxFnKARPQQ/meSIHrziZQjzIA/ZnMRMeG0QpjY2zDbb5cbMqz3S6j/5NdZe1cWX1pg0XrGzrfbxSRzrJKkqeYZ6milulatiM0z4cmrcufxndVDkKnpNO5VTVYt6aoW/oYyRDtX8Zcgx2f0VV7OPlktVM5/Iau7i5JWI5SFvWm@d/NiGYFHMk@FLy4cj/wqXJtqHLWhF3PugbL3sc@P/JhJw3l@IxEpM1KSYFRUuWWL@iA9qeO8a7y1O4W2iePcjLMY@H7I25@N3EF0Vxy64B66N1@Kfk4qN1SZsvZu84OwX "JavaScript (Node.js) – Try It Online")
  • ## Test Cases
  • Here is a test case, sorting first by last name then by first name.[^1]
  • ```
  • Christina Johnson
  • Steward Johnson
  • Steward White
  • Steward O'Brian
  • Steward Smith
  • Bill Smith
  • John Johnson
  • James Smith
  • Sally Johnson
  • Christina Meyers
  • Chris Meyers
  • Steward Meyers
  • Bill O'Brian
  • Zachary Smith
  • Chris Brown
  • Zachary O'Brian
  • Abbey Smith
  • Zachary Meyers
  • John Brown
  • Sally Smith
  • Zachary Johnson
  • Chris White
  • =>
  • Chris Brown
  • John Brown
  • Christina Johnson
  • John Johnson
  • Sally Johnson
  • Steward Johnson
  • Zachary Johnson
  • Chris Meyers
  • Christina Meyers
  • Steward Meyers
  • Zachary Meyers
  • Bill O'Brian
  • Steward O'Brian
  • Zachary O'Brian
  • Abbey Smith
  • Bill Smith
  • James Smith
  • Sally Smith
  • Steward Smith
  • Zachary Smith
  • Chris White
  • Steward White
  • ```
  • Here is another test case sorting first by the `x` property, then the `y` property, then the `z` property:
  • ```
  • Comparators:
  • c1 = (a, b) => a.x - b.x
  • c2 = (a, b) => a.y - b.y
  • c3 = (a, b) => a.z - b.z
  • Input:
  • [ { x: 1, y: 2, z: 2 },
  • { x: 3, y: 2, z: 3 },
  • { x: 2, y: 2, z: 3 },
  • { x: 1, y: 1, z: 3 },
  • { x: 2, y: 2, z: 1 },
  • { x: 1, y: 2, z: 1 },
  • { x: 1, y: 2, z: 3 } ]
  • Output:
  • [ { x: 1, y: 1, z: 3 },
  • { x: 1, y: 2, z: 1 },
  • { x: 1, y: 2, z: 2 },
  • { x: 1, y: 2, z: 3 },
  • { x: 2, y: 2, z: 1 },
  • { x: 2, y: 2, z: 3 },
  • { x: 3, y: 2, z: 3 } ]
  • ```
  • This is <a class="badge is-tag" href="https://codegolf.codidact.com/categories/49/tags/4274">code-golf</a>, so the entry with the lowest bytes wins!
  • [^1]: Incidentally, I generated this data with a short script
#1: Initial revision by user avatar Moshi‭ · 2021-09-01T21:29:02Z (over 2 years ago)
Generalized Sort
# Challenge

We all know and love the generic sort function, right? However, it only sorts based off one criterion - what if we want more? That's where you come in.

Your task is to sort an array based off an arbitrary number of comparison functions `c1`, `c2`, `c3`, etc.; first sort by comparing using `c1`, if there is a tie then sort by `c2`, if there is still a tie sort by `c3`, etc. Output order is undefined if none of the comparison functions breaks the tie.

## Example

Here is an example in JavaScript:

```javascript
function sort(array, ...comparators) {
	return array.sort((a, b) =>
		comparators.map(c => c(a, b)) // Apply comparisons
		           .find(r => r != 0)  // Find the first non-zero comparison
	);
}
```

[Try it online!](https://tio.run/##dZJNb8IwDIbP9Fd4XGg1CB/XiUmAtAPStAOHSWMcTAhrUEkqJwyxab@9c1sYtLBKTdU3z2s7sTf4iU6STn3H2JXKsvXOSK@tAWfJh0iEhzYIIaTdpkjoLbkIvoMGKb8jAwUgCjbENiwjGD4GjcYFLbaYhpJlkCURQbcLozRNDlBi2lnj2ATnR6y1WYWUuwjuhtCLIHc9sQo@VrDW5DwYazpfiuxFnKARPQQ/meSIHrziZQjzIA/ZnMRMeG0QpjY2zDbb5cbMqz3S6j/5NdZe1cWX1pg0XrGzrfbxSRzrJKkqeYZ6milulatiM0z4cmrcufxndVDkKnpNO5VTVYt6aoW/oYyRDtX8Zcgx2f0VV7OPlktVM5/Iau7i5JWI5SFvWm@d/NiGYFHMk@FLy4cj/wqXJtqHLWhF3PugbL3sc@P/JhJw3l@IxEpM1KSYFRUuWWL@iA9qeO8a7y1O4W2iePcjLMY@H7I25@N3EF0Vxy64B66N1@Kfk4qN1SZsvZu84OwX "JavaScript (Node.js) – Try It Online")

## Test Cases

Here is a test case, sorting first by last name then by first name.[^1]

```
Christina Johnson
Steward Johnson
Steward White
Steward O'Brian
Steward Smith
Bill Smith
John Johnson
James Smith
Sally Johnson
Christina Meyers
Chris Meyers
Steward Meyers
Bill O'Brian
Zachary Smith
Chris Brown
Zachary O'Brian
Abbey Smith
Zachary Meyers
John Brown
Sally Smith
Zachary Johnson
Chris White

=>

Chris Brown
John Brown
Christina Johnson
John Johnson
Sally Johnson
Steward Johnson
Zachary Johnson
Chris Meyers
Christina Meyers
Steward Meyers
Zachary Meyers
Bill O'Brian
Steward O'Brian
Zachary O'Brian
Abbey Smith
Bill Smith
James Smith
Sally Smith
Steward Smith
Zachary Smith
Chris White
Steward White
```

This is <a class="badge is-tag" href="https://codegolf.codidact.com/categories/49/tags/4274">code-golf</a>, so the entry with the lowest bytes wins!

[^1]: Incidentally, I generated this data with a short script