Sorting comma delimited items on one line

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
oldummy
Posts: 97
Joined: Mon Jun 13, 2016 3:21 pm

Sorting comma delimited items on one line

Post by oldummy » Sun Jun 09, 2024 11:53 am

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.

SWEdeAndy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 324
Joined: Sat Aug 16, 2008 9:48 am
Contact:

Re: Sorting comma delimited items on one line

Post by SWEdeAndy » Sun Jun 09, 2024 12:42 pm

Code: Select all

replace comma with return in mop
sort mop numeric
replace return with comma in mop
Andreas Bergendal
Independent app and system developer
Free LC dev tools: https://github.com/wheninspace
(WIS_WebDeployHelper, WIS_ScriptDependencies, WIS_BrowserAnimation)
WhenInSpace: https://wheninspace.com

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Sorting comma delimited items on one line

Post by SparkOut » Sun Jun 09, 2024 12:44 pm

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

oldummy
Posts: 97
Joined: Mon Jun 13, 2016 3:21 pm

Re: Sorting comma delimited items on one line

Post by oldummy » 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 ]

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Sorting comma delimited items on one line

Post by FourthWorld » Sun Jun 09, 2024 7:45 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting comma delimited items on one line

Post by dunbarx » Mon Jun 10, 2024 12:22 am

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

Post Reply