Different alphabetical orders

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

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

Re: Different alphabetical orders

Post by jacque » Sat Apr 27, 2013 7:08 pm

Is this what you need?:

Code: Select all

on mouseUp
  put ")/andra moi e)/nnepe, Mou=sa, polu/tropon, o(\s ma/la polla\  pla/gxqh, e)pei\ Troi/hs i(ero\n ptoli/eqron e)/perse:" into test
  replace space with cr in test
  replace comma with empty in test
  put "abgdezhqiklmnxoprstufxywABGDEZHQIKLMNXOPRSTUFXYW" into tOrder
  sort lines of test by customSort(tOrder,each)
  put test
end mouseUp

function customSort whichOrder,theWord
  set the casesensitive to true
  repeat for each char c in theWord
    put offset(c,whichOrder) into tGreekOrder
    get char tGreekOrder of "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    if it = "" then put c after tNewString
    else put it after tNewString
  end repeat
  return tNewString
end customSort
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Different alphabetical orders

Post by jacque » Sat Apr 27, 2013 7:41 pm

Brevity for compulsives:

Code: Select all

function customSort whichOrder,theWord
  set the casesensitive to true
  repeat for each char c in theWord
    get char offset(c,whichOrder) of "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    if it = "" then put c after tNewString
    else put it after tNewString
  end repeat
  return tNewString
end customSort
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Different alphabetical orders

Post by dunbarx » Sat Apr 27, 2013 8:30 pm

Hi.

My script works.

Jacque has given a custom sort function, and she usually knows what she is doing. Does her script handle diacriticals?

But since you have worked so hard, I will go and write a restore gadget. I only ask that you read it and see how it works. You seem intent, and so I hope you put the effort in. The best lesson here is to do just what you are doing. Fooling around with all kinds of methods.

I have a button and three fields: The fields are "data", which holds your raw data, "code" which will display the raw numeric translation, and "sorted" which will display the sorted data.

In the button script:

Code: Select all

on mouseUp
      put "ABGDEZHQIKLMNX" into latinString  --sort order of chars
      put fld "data" into toSort
      
      repeat with y = 11 to 24 --just not to have to deal with "03", for example
            put y into latin[char (y - 10) of latinString]
      end repeat
      
      put toSort into newString
      repeat for each char tChar in toSort
            if tChar = return then next repeat
            replace tChar with latin[tChar] in newString
      end repeat
      
   Sort newString numeric by char 1 to 2 of each & char 3 to 4 of each & char 5 to 6 of each & char 7 to 8 of each
   put newString into fld "code"
   
   --here is the decoding

   repeat with y = 1 to the number of chars of latinString --make a correspondence between the chars and their sort order
      put y + 10 & "," & char y of latinstring & return after temp
   end repeat
   
   replace return with "99" in newstring  --now why do this?
   put 1 into counter
   repeat until counter > the number of chars of newString
      put char counter to (counter +1) of newstring into tChar
      if tChar = 99 then put return after restoredText
      put item 2 of line tChar - 10 of temp after restoredText
      add 2 to counter
   end repeat
   put restoredText into fld "sorted"
end mouseUp
There are many places where this can be made more compact. A great lesson for you would be to use an array in place of the "temp" thing I threw together. It would be a switch of the keys and elements of the array "latin". Anyway, if you step through the last repeat, you should see how easy it is to decode the pairs of numbers.

Craig Newman

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

Re: Different alphabetical orders

Post by jacque » Sun Apr 28, 2013 6:50 pm

dunbarx wrote: Does her script handle diacriticals?
Oops, quite right, it doesn't. Just to round out my entry:

Code: Select all

sort lines of test international by customSort(tOrder,each)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Different alphabetical orders

Post by Simon » Sun Apr 28, 2013 8:00 pm

I knew this would be Fun!

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

danielrr
Posts: 142
Joined: Mon Mar 04, 2013 4:03 pm

Re: Different alphabetical orders

Post by danielrr » Sun Apr 28, 2013 8:45 pm

jacque wrote:Is this what you need?:

Code: Select all

on mouseUp
  put ")/andra moi e)/nnepe, Mou=sa, polu/tropon, o(\s ma/la polla\  pla/gxqh, e)pei\ Troi/hs i(ero\n ptoli/eqron e)/perse:" into test
  replace space with cr in test
  replace comma with empty in test
  put "abgdezhqiklmnxoprstufxywABGDEZHQIKLMNXOPRSTUFXYW" into tOrder
  sort lines of test by customSort(tOrder,each)
  put test
end mouseUp

function customSort whichOrder,theWord
  set the casesensitive to true
  repeat for each char c in theWord
    put offset(c,whichOrder) into tGreekOrder
    get char tGreekOrder of "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    if it = "" then put c after tNewString
    else put it after tNewString
  end repeat
  return tNewString
end customSort

Woah!. my jaw drops! this single line:

Code: Select all

sort lines of test by customSort(tOrder,each)
is like a a window to a new world for me. Thanks so much.

Besides, the function is amazingly fast. Orders of magnitude faster than mine. Your function orders alphabetically all the words of the first book of Homer's Odyssey (about 3200 words) in just 34 milliseconds (0.010423 ms per word); My function slugs at 489 ms (0.149908 per word)

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Different alphabetical orders

Post by Simon » Sun Apr 28, 2013 9:27 pm

I just copied and pasted some text from this post and ran Jacque's script and got this:

are
around
ask
best
Does
button
gadget.
given
go

How did that "Does" get in there?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

danielrr
Posts: 142
Joined: Mon Mar 04, 2013 4:03 pm

Re: Different alphabetical orders

Post by danielrr » Sun Apr 28, 2013 9:47 pm

dunbarx wrote:Hi.

My script works.

Jacque has given a custom sort function, and she usually knows what she is doing. Does her script handle diacriticals?

But since you have worked so hard, I will go and write a restore gadget. I only ask that you read it and see how it works. You seem intent, and so I hope you put the effort in. The best lesson here is to do just what you are doing. Fooling around with all kinds of methods.

I have a button and three fields: The fields are "data", which holds your raw data, "code" which will display the raw numeric translation, and "sorted" which will display the sorted data.

In the button script:

Code: Select all

on mouseUp
      put "ABGDEZHQIKLMNX" into latinString  --sort order of chars
      put fld "data" into toSort
      
      repeat with y = 11 to 24 --just not to have to deal with "03", for example
            put y into latin[char (y - 10) of latinString]
      end repeat
      
      put toSort into newString
      repeat for each char tChar in toSort
            if tChar = return then next repeat
            replace tChar with latin[tChar] in newString
      end repeat
      
   Sort newString numeric by char 1 to 2 of each & char 3 to 4 of each & char 5 to 6 of each & char 7 to 8 of each
   put newString into fld "code"
   
   --here is the decoding

   repeat with y = 1 to the number of chars of latinString --make a correspondence between the chars and their sort order
      put y + 10 & "," & char y of latinstring & return after temp
   end repeat
   
   replace return with "99" in newstring  --now why do this?
   put 1 into counter
   repeat until counter > the number of chars of newString
      put char counter to (counter +1) of newstring into tChar
      if tChar = 99 then put return after restoredText
      put item 2 of line tChar - 10 of temp after restoredText
      add 2 to counter
   end repeat
   put restoredText into fld "sorted"
end mouseUp
There are many places where this can be made more compact. A great lesson for you would be to use an array in place of the "temp" thing I threw together. It would be a switch of the keys and elements of the array "latin". Anyway, if you step through the last repeat, you should see how easy it is to decode the pairs of numbers.

Craig Newman
It is true, Craig. Your script works (although it doesnt solve the problem with the upperccase and lower case disctintion). And it is more than 10 times faster than mine and I don't quite why. My approach mean two iterations per line (one of them with a condition) and yours one iteration per character. And still, more than ten times faster. (only thing in favour of mi function is that it distinguishes upper and lower case).

Is there a simple way to switch keys and elements from one array?
many thanks again

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

Re: Different alphabetical orders

Post by jacque » Sun Apr 28, 2013 10:08 pm

Yeah, custom sorts are very powerful.

Simon, what text did you copy? I'll try it too.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Different alphabetical orders

Post by Simon » Sun Apr 28, 2013 10:16 pm

Hi Jacque,
From Craig's post:
Hi.

My script works.

Jacque has given a custom sort function, and she usually knows what she is doing. Does her script handle diacriticals?

But since you have worked so hard, I will go and write a restore gadget. I only ask that you read it and see how it works. You seem intent, and so I hope you put the effort in. The best lesson here is to do just what you are doing. Fooling around with all kinds of methods.

I have a button and three fields: The fields are "data", which holds your raw data, "code" which will display the raw numeric translation, and "sorted" which will display the sorted data.
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Different alphabetical orders

Post by jacque » Sun Apr 28, 2013 10:40 pm

I see, I think the "Does" is in the correct order. It is sorting at position "B". The custom sort function finds the numerical offset of the character in the custom order list, and returns the english alphabetic equivalent so that the engine can sort the words using standard (english) sorting routines.

In the custom order list, a "D" is character #28. In the english sorting list, character #28 is "B". When the engine sorts the text that is returned, it sorts it into the "B" slot.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Different alphabetical orders

Post by dunbarx » Sun Apr 28, 2013 10:51 pm

I disagree.

The method I first thought of, to map chars to numbers, will work with any sort of char, or for that matter, any sort of element at all. The sort set could be feelings of inadequacy, photos of different types of zebra, or characters, containing diacriticals, upper and lower case, mixed alphabets, whatever.

The mapping says:

Take the first element and give it an "11". Take the second element and give it a "12". Etc. Sort numerically, and restore the original set.

There need be no correlation at all among the elements of the set to be sorted, and that is what made me think that path had value. So upper and lower case is implicitly managed, though the sort order must be explicitly declared. But the method obviates any possible native issues with the set to be sorted.

I wonder if there are uses beyond sorting chars, for example, one could sort colors according to mood, there being no alphabetic relationship at all. Is this valuable?

Craig Newman

Post Reply