Sorting Numbers and Text

Discuss the spreadsheet application
Post Reply
noibs
Posts: 11
Joined: Fri Oct 17, 2008 3:58 pm

Sorting Numbers and Text

Post by noibs »

Assume the following list:

4
6
1
5
M
9
3

What I want is to sort that list in Calc so that the "M" character is considered to be less than the lowest number in the list. Therefore, if the list is sorted in ascending order, the "M" would be first followed by the 1. In descending order, the "M" would be the last item in the list.

However, Calc always sorts with the "M" treated as being larger than any number in the list. Custom sort lists can only contain text, so that doesn't work. Even if i put a zero in front of the M--0M--Calc considers that to be text and sorts it as something larger than the 9.

Any ideas? Enabling "Natural Sort" has no effect.
Dave
Posts: 1011
Joined: Sun Dec 23, 2007 6:53 pm

Re: Sorting Numbers and Text

Post by Dave »

Perhaps it does so because it IS larger? With a character in A1, put =CODE(A1) in B1 and you'll see.

David.
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Sorting Numbers and Text

Post by acknak »

(Hi Dave! Nice to see you again!)

When you need to modify the sort order of a data set, the most flexible and powerful way to do it is to add a column that contains a sort key computed from the data that you need to sort. Because you compute the sort key, you can produce (almost) any order you want.

And Dave's suggestion would be one way to do it: use CODE() to convert the data into numbers, and adjust the number when there is an "M": =IF(A1="M";0;CODE(A1)) Each digit will give a number from 48 to 57; "M" will give zero. Sort the data on the sort key column and you're done.
AOO4/LO5 • Linux • Fedora 23
Post Reply