Page 1 of 1

Sorting comma delimited items on one line

Posted: Sun Jun 09, 2024 11:53 am
by oldummy
I have been trying to sort 10 comma delimited numbers on one line ,lowest to highest.

I have put the line of comma delimited number into a variable (call it mop) and tried:

sort mop numeric
or:
sort mop numeric ascending.

I thought I had used this years ago, but it seems not.

Every article I have read seems to me to be dealing with lines in a field.

I am feeling even more thickheaded than usual with this.

Re: Sorting comma delimited items on one line

Posted: Sun Jun 09, 2024 12:42 pm
by SWEdeAndy

Code: Select all

replace comma with return in mop
sort mop numeric
replace return with comma in mop

Re: Sorting comma delimited items on one line

Posted: Sun Jun 09, 2024 12:44 pm
by SparkOut
The dictionary does specify the syntax you need, and the various options.

sort [{lines | items} of] container [direction] [sortType] [by sortKey ]

If you leave out the chunk type, LC will expect you to mean "lines". For another chunk type, just use it in the statement.

Dictionary example:

local tNumbers
put "5,4,3,2,1" into tNumbers
sort items of tNumbers ascending numeric -- 5,4,3,2,1 becomes 1,2,3,4,5

Re: Sorting comma delimited items on one line

Posted: Sun Jun 09, 2024 6:24 pm
by oldummy
Sorry, but I have trouble making sense of the way things are answered in the dictionary.

This for instance seems reading another language:

sort [{lines | items} of] container [direction] [sortType] [by sortKey ]

But both of you solutions worked fine, and I do thank you both for the answers.

Re: Sorting comma delimited items on one line

Posted: Sun Jun 09, 2024 7:45 pm
by FourthWorld
oldummy wrote:
Sun Jun 09, 2024 6:24 pm
Sorry, but I have trouble making sense of the way things are answered in the dictionary.

This for instance seems reading another language:

sort [{lines | items} of] container [direction] [sortType] [by sortKey ]
For historical reasons, LC docs use a simplified variant of the Backus–Naur form (BNF) of syntax definition, where:

- Brackets ("[" and "]") enclose optional clauses.

- Vertical pipe ("|") means "or".

- Curly braces ("{" and "}") enclose options where at least one is required.

I believe these and other conventions used in the syntax guides are described in the User Guide (accessible through an item near the top of the Help menu), and may also be included in the API Dictionary itself. I'm not at a computer right now so I can't point to the precise location of that explanation, but the User Guide's Table of Contents is pretty good and should point the way to definitions for such conventions used throughout LC docs.

Re: Sorting comma delimited items on one line

Posted: Mon Jun 10, 2024 12:22 am
by dunbarx
What everyone said.

So, in "mop", do you indeed have a series of numbers separated by commas on a single line? Like "204,21,42,99"? If so, and I bet you already have gotten this, but just to make sure, you would:

Code: Select all

sort items of mop numeric
Remember that you can add "descending" if you need to, but "ascending" is the default, so the sort will start with the lowest

Craig