Calc: Sorting numbers ("Text") pasted from a text editor

I have a column with cells consisting of whole numbers (from 1 to 9999) that should be sorted in ascending order. Occasionally there is a cell containing two numbers joined by a hyphen, where the second of these numbers is higher than the first.

The settings used are:

View > Value Highlighting: ON

Format, Cells > Numbers: Text

Data, Sort > Options:

Include formats: OFF

Enable natural sort: ON

Language: Default - English (UK)

So (1) if I type in the numbers directly into a Calc column and do a sort, the result is as expected:

image description

However, (2) if I type in the numbers in a text editor and then paste them into a column, the cells containing the hyphens stay black but those without hyphens turn blue, and the result is not as expected:

image description

Question: How can I get with (2) results as (1)?

the cells containing the hyphens stay black but those without hyphens turn blue,

… black characters indicate data type “Text” and you try to sort numbers and text simultaneously and numbers get sorted first and then text gets sorted. I’m not aware of any sort option to achieve your desired result (in that sense: The title of the question is incorrect, since in fact your problem is not about “Sorting numbers…”)

1 Like

When you paste from the text editor, you will see the Text Import dialog, select the columns that you want to be Text and then in Column type select Text. You will be able to sort the “numbers” using Natural sort order as they are text. See @anon73440385 comment about data type. Cheers, Al

1 Like

Thank you! I was not aware that you can select the columns and then define the column type. But doing so will prevent future problems.

The text Import dialog is very important; many questions relate to the effects of ignoring it.

Hello,

according to my comment you need to create a helpers column (sort column) and sort by the values in the helpers data column: For the helper column B use formula:

=VALUE(REGEX(A1;"\d+(?-)")) (assuming your list starts in column A, row 1; this formula creates a numeric value for all cells by cutting everything from the first digit through the hyphen and allows to perform a numeric sort).

Now select range columnsA and B and sort by column B and you get:

image description

Hope that helps.

… too complicated: =TEXT(A1;"@") and natural sort would work as well.

Excellent, thank you! – I’ve changed the title.

You may also use Data -> Text to Columns after importing and finally end up at @EarnestAl’s answer, which is the far better way to take precautions instead of fixing issues afterwards.

@anon73440385, =TEXT(A1;"@") is working as expected, but in one cell it does not: “5-8” yields “44413”. Why? (The formula is the same for all cells.)

… obviously you have a locale, which uses a date acceptance pattern D-M... and due to that =TEXT(A1;"@") shows the real value as text (44413 is date 2021-08-05). To be honest: I did not expect that to happen, and for me it is an inconsistency that text gets turned to number on using function TEXT() explicitly (regardless of any date acceptance pattern).

See Tools -> Options -> Language Settings -> Languages -> Date acceptance patterns:

“(?-)” matches the remainder of the pattern with the active flags. There are no flags. And we are not interested in the rest of the string.
Isn’t it easier and faster this way? “^\d+” .

Maybe you meant this (look forward): “\d+(?=-)” ? But this is not necessary.

This did not affect the correctness of the expression in any way.

1 Like

… of course you are right. That’s what I meant.

The anchor on the left will speed up the search.

I use notepad++ to sort and do text based things and a lot of other things that I either can’t figure out how to do with office software or that office software just doesn’t do.

There’s always some weird way they do something that doesn’t produce the result I expect it should. I suggest using notepad++ to edit and sort plain text first. That is what I do. Notepad++ has a ton of options office suite software does not have therefore doesn’t do. To sort the numbers correctly / as I expected them to be as I assume you expect them to be in notepad++ select Edit > Line Operations > Sort Lines As Integers Ascending.

I mixed the data you provided up so it was originally:

 3
132
24-30
32
293
5-8
281-306
122-125

I then copied the mixed up data from Calc to Notepad++ and selected Edit > Line Operations > Sort Lines As Integers Ascending and got:

3
5-8
24-30
32
122-125
132
281-306
293

Why office suite text editors do the weird stuff like it did here is probably the same reason coders don’t use office suite text editors to write programming code: office suite writers include a lot of code to files and text we don’t see. That code office suite text editors include is helpful for printing and other office suite text programs. It can have an unexpected or unappealing effect on text copied from an office suite text editor to another program.

For web-based developers (my background) using office suite programs to write code is the ultimate sin. Writing web code that’s going to display on a website in an office suite text editor almost never goes well.

Use a plain text editor that has a sort feature. I find myself using notepad++ often in conjunction with calc. I use ++ to automatically remove links and images before transfering something I copied from somewhere to calc. I also use ++ for sorting because while there may be a way to get calc to sort in situations like this as expected I personally don’t know how to get it to do it.

Thanks. I’m on macOS so I usually use BBEdit to prepare data to paste into Calc.

Did you find a way to get BBEdit to do this? I don’t have any Apple devices so I’m not able to look in to it. Perhaps you might want to use an emulator so you can also run windows programs on your mac? It’s an idea and suggestion. I cringe at emulators myself, personally, but…

A lot of people I know that decided to get an Apple device end up using an emulator to run windows because they can’t get what they want to do to work on their Mac. It’s an option that will provide a solution.

Yes, BBEdit has a “Numbers match by value” sort option, so the result is the same you get with Notepad++. No need to use an emulator … I once installed one, but it worked not as expected, so I deleted it again. Never missed once since. :slight_smile:

I’m not sure if this will be helpful to you - you didn’t say what you are going to do with these values further. However, the look of your data reminded me of the ListOK extension. If in the end you want to get an ordered list like 3, 5-8, 24-30, 32, 122-125, 132, 281-306 (You see? 293 disappeared from the list because it is already in 281-306), then a function =LSTARR(A2:A9) will return exactly this result. If you need an ordered list of all the numbers in the list, the {=LSTVAL(LSTARR(A2:A9))} formula will help.

ListOK_sample

2 Likes

Great! Thank you, this is very helpful indeed. I’ll need to further study this to see how I can use it in my spreadsheet.