Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 945021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:41:14+00:00 2026-05-15T22:41:14+00:00

Suppose I have a list of things (numbers, to keep things simple here) and

  • 0

Suppose I have a list of things (numbers, to keep things simple here) and I have a function I want to use to sort them by, using SortBy.
For example, the following sorts a list of numbers by last digit:

SortBy[{301, 201}, Mod[#,10]&]

And notice how two of (ie, all of) those numbers have the same last digit.
So it doesn’t matter which order we return them in.
In this case Mathematica returns them in the opposite order.
How can I ensure that all ties are broken in favor of how the items were ordered in the original list?

(I know it’s kind of trivial but I feel like this comes up from time to time so I thought it would be handy to get it on StackOverflow. I’ll post whatever I come up with as an answer if no one beats me to it.)

Attempts at making this more searchable: sort with minimal disturbance, sort with least number of swaps, custom tie-breaking, sorting with costly swapping, stable sorting.

PS: Thanks to Nicholas for pointing out that this is called stable sorting. It was on the tip of my tongue! Here’s another link: Link

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:41:15+00:00Added an answer on May 15, 2026 at 10:41 pm

    After asking around, I was given a satisfying explanation:

    Short answer: You want SortBy[list, {f}] to get a stable sort.

    Long answer:

    SortBy[list, f] sorts list in the order determined by applying f to each element of list, breaking ties using the canonical ordering explained under Sort. (This is the second documented “More Information” note in the documentation for SortBy.)

    SortBy[list, {f, g}] breaks ties using the order determined by applying g to each element.

    Note that SortBy[list, f] is the same as SortBy[list, {f, Identity}].

    SortBy[list, {f}] does no tie breaking (and gives a stable sort), which is what you want:

    In[13]:= SortBy[{19, 301, 201, 502, 501, 101, 300}, {Mod[#, 10] &}]
    
    Out[13]= {300, 301, 201, 501, 101, 502, 19}
    

    Finally, sakra’s solution SortBy[list, {f, tie++ &}] is effectively equivalent to SortBy[list, {f}].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my problem. Suppose I have a table called persons containing, among other things,
Suppose I have a list of dates in the string format, 'YYYYMMDD.' How do
Suppose I have a list, in which no new nodes are added or deleted.
Suppose I have a list (or Set): List<String> testList = Lists.newArrayList(assocX,srcT,destA,srcX, don't care Y,
Suppose I have a list X = [a, b, c] where a, b, c
Suppose I have a list that I wish not to return but to yield
Suppose I have a list of #define s in a header file for an
Suppose you have a LIST datatype in Redis. How do you delete all its
Suppose I have a list box with different items, each with unique values, and
Suppose I have a list like this <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.