Different sort of sort

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
RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Different sort of sort

Post by RossG » Mon Oct 17, 2016 10:17 pm

I have lines in a field...

H,2.1
R,1.2
B,4.2
L,3.1
O,7.7
E,3.4

I want to sort them in order R,B,H,L,O,E.

How can I sort this way?
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Different sort of sort

Post by jacque » Tue Oct 18, 2016 8:03 pm

You need a custom sort function for something like this. Assuming a field that contains the data:

Code: Select all

on customSort
  put fld 1 into tList -- you could use a variable here instead
  put "R,B,H,L,O,E" into tOrderList
  split tOrderList by comma
  sort lines of tList by sortIt(each,tOrderList)
  -- do something with the sorted list here
end customSort

function sortit tLine,tOrderList
  repeat for each key k in tOrderList
    if tOrderList[k] = item 1 of tLine then
      return k
    end if
  end repeat
end sortit
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: Different sort of sort

Post by rkriesel » Tue Oct 18, 2016 9:36 pm

Hi, RossG.

You could sort the field using an array:

Code: Select all

repeat for each item k in "R,B,H,L,O,E"
  add 1 to i
  put i into a[ k ]
end repeat
sort fld 1 by a[ item 1 of each ]
-- Dick

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Different sort of sort

Post by [-hh] » Tue Oct 18, 2016 10:04 pm

This is an interesting problem:
What to do with lines, that have something else as a first item (missing data/empty, typo)?
Here is a variant that includes this aspect.

Code: Select all

local orderList = "R,B,H,L,O,E"
sort myVar numeric by itemoffset(item 1 of each, orderList)
[The 'numeric' is only needed in case you have more than 9 items in your orderList]
shiftLock happens

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

Re: Different sort of sort

Post by dunbarx » Tue Oct 18, 2016 11:47 pm

@ Jacque, Dick.

Nice.

@Hermann.

Really nice.

All three of these should be combined as three instances of a solution to a problem, in a lesson in their own right.

Craig

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Different sort of sort

Post by RossG » Wed Oct 19, 2016 1:42 am

dunbarx wrote:@ Jacque, Dick.

Nice.

@Hermann.

Really nice.

All three of these should be combined as three instances of a solution to a problem, in a lesson in their own right.

Craig
What I would have said if I hadn't been so admiring of these
solutions that I was left "speechless". Three cheers and a cigar to each. Thank you.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

Post Reply