sorting an array by word length

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Hendricus
Posts: 7
Joined: Tue Aug 18, 2020 6:03 pm

sorting an array by word length

Post by Hendricus » Tue Aug 18, 2020 6:13 pm

Can someone on the forum give me a hint what I am missing in my array sort code.

I have a list of names that need to ordered by the name length, starting with the longest name. It is strange but it seems to have a few names that are not being picked up properly and I cannot see why.

Code: Select all

function sortByLength @pArray
   local tSortedArray
   local tNextIndex
   
   get the keys of pArray
   put the number of elements of pArray into tCount
   sort lines of it descending by the length of pArray[each]
   
   split it by return
   
   put 1 into tNextIndex
   repeat for each element tIndex in it
      put pArray[tIndex] into tSortedArray[tNextIndex]
      add 1 to tNextIndex
   end repeat
   
   return tSortedArray
end sortByLength

on openCard
   put "Aaron,Abel,Abigail,Abraham,Enoch,Jeroboam,Benjamin,Dorcas,Isaac,Ishmael," &\
         "Job,Joseph,Lazarus,Luke,Mark,Mary,Miriam,Moses,Noah,Reuben,Nehemiah," &\
         "Jezebel,Jehovah,Jethro,Judas,Laban,Samuel,Saul,Solomon,Thomas,Timothy," &\
         "Uriah,Zechariah,Potiphar,Mordecai,Rachel,Deborah,Cornelius,Belshazzar," &\
         "Bathsheba,Andrew,Amos,Amnon,Adam,Achan,Nathan,Ezra,Rahab,Paul,Martha,Ahab," &\
         "Levi,Jehu,Jonathan,Jonah,Rehoboam,Shem,Elijah,Caiaphas,Jacob,Ezekiel,Nimrod," &\
         "Nebuchadnezzar,John,Balaam,Barnabas,Hezekiah,Lot,Gideon,Eli,David,Joshua," &\
         "Melchizedek,Esther,Sarah,Zedekiah,Elisha,Nicodemus,Isaiah,Ruth,Zacchaeus," &\
         "Rebekah,Samson,Nathanael,Matthew,Hagar,Josiah,Hiram,James,Absalom,Caleb," &\
         "Ahasuerus,Pharaoh,Jephthah,Lamech,Philip,Cain,Gabriel,Hosea,Esau,Jesus,Peter," &\
         "Eve,Daniel" into tNameList
   
   put empty into tArray
   put 1 into tCounter
   set itemdel to comma
   repeat for each item tWord in tNameList
      put tWord into tArray[tCounter]
      add 1 to tCounter
   end repeat
   put sortByLength(tArray) into tArraySorted
   
   put empty into tList1
   repeat for each element tWord in tArray
      put tWord & CR after tList1
   end repeat
   put tList1 into field "Field1"
   
   put empty into tList2
   repeat for each element tWord in tArraySorted
      put tWord & CR after tList2
   end repeat
   put tList2 into field "Field2"
end openCard

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: sorting an array by word length

Post by dunbarx » Tue Aug 18, 2020 8:37 pm

Hi.

Several things I see are wrong with your handlers. But I do see a VERY good basic grasp of LC.

For example, when you sorted the local variable "it", which only contains the elements of the array, you have broken the association to that original array.

Anyway, why not just:
sort items of tNameList by the length of each
I did not go into your work in detail, so I may not appreciate any other functionality associated with the two handlers. But can't you make a better start with the above? And if you need to associate the sorted list later, perhaps with the length of each name, you can do something like:

Code: Select all

  repeat for each item tItem in tNameList
      put tItem into newArray[the length of tItem]
   end repeat
But I want to hear what you think about all this, so get back as soon as you can, either telling me where I missed it, or where you did.

BOGS!!!

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: sorting an array by word length

Post by dunbarx » Tue Aug 18, 2020 8:55 pm

One thing you also must be aware of, and that is the hidden trap when sorting with numeric sortKeys. Consider this:

Code: Select all

on mouseUp
   put "Belshazzar,Melchizedek,Nebuchadnezzar,Job,Hezekiah,Eli" into tNameList
   sort items of tNameList by the length of each
   breakpoint
   sort items of tNameList numeric by the length of each
   breakpoint
end mouseUp
In the first sort the longest names come before shorter ones. That is because the sortKey "length of each" gives a value of 10 for "Balshazzar", 11 for "Melchizedek" and 14 for "Nebuchadnezzar". When you sort by the length, those values are placed into the local variable "each", and the sort is taken from their textual values, not their numeric values. "14" comes before, say, "3", the length of "Job".

Do you see? ASCII "14" comes before ASCII "3", because '1" comes before "3".

The second sort fixes this. The numbers are treated as, er, numbers, and 3 comes before 14.

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: sorting an array by word length

Post by bn » Tue Aug 18, 2020 9:23 pm

Hi Hendricus,

here is something along the lines of what Craig said

Code: Select all

on openCard
   put "Aaron,Abel,Abigail,Abraham,Enoch,Jeroboam,Benjamin,Dorcas,Isaac,Ishmael," &\
         "Job,Joseph,Lazarus,Luke,Mark,Mary,Miriam,Moses,Noah,Reuben,Nehemiah," &\
         "Jezebel,Jehovah,Jethro,Judas,Laban,Samuel,Saul,Solomon,Thomas,Timothy," &\
         "Uriah,Zechariah,Potiphar,Mordecai,Rachel,Deborah,Cornelius,Belshazzar," &\
         "Bathsheba,Andrew,Amos,Amnon,Adam,Achan,Nathan,Ezra,Rahab,Paul,Martha,Ahab," &\
         "Levi,Jehu,Jonathan,Jonah,Rehoboam,Shem,Elijah,Caiaphas,Jacob,Ezekiel,Nimrod," &\
         "Nebuchadnezzar,John,Balaam,Barnabas,Hezekiah,Lot,Gideon,Eli,David,Joshua," &\
         "Melchizedek,Esther,Sarah,Zedekiah,Elisha,Nicodemus,Isaiah,Ruth,Zacchaeus," &\
         "Rebekah,Samson,Nathanael,Matthew,Hagar,Josiah,Hiram,James,Absalom,Caleb," &\
         "Ahasuerus,Pharaoh,Jephthah,Lamech,Philip,Cain,Gabriel,Hosea,Esau,Jesus,Peter," &\
         "Eve,Daniel" into tNameList
   
   replace comma with return in tNameList
   put tNameList into field "field1"
   sort tNameList numeric descending by length(each)
   put tNameList into field "field2"
end openCard
Kind regards
Bernd

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: sorting an array by word length

Post by bogs » Tue Aug 18, 2020 9:47 pm

I think you called the wrong guy, Craig, you and Bernd have it well in hand, as far as I can tell ;)
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: sorting an array by word length

Post by dunbarx » Tue Aug 18, 2020 10:23 pm

Boggsie.

I only do that sort of nonsense after telling an OP to get back to us. You were the one who called me out for that. :wink:

Craig

Hendricus
Posts: 7
Joined: Tue Aug 18, 2020 6:03 pm

Re: sorting an array by word length

Post by Hendricus » Tue Aug 18, 2020 10:46 pm

Thanks, Craig. I was not aware of the "numeric" keyword I had to put in. It makes much sense. I learned something important here. :D

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: sorting an array by word length

Post by bogs » Wed Aug 19, 2020 10:20 am

"Called you out" seems so ... violent, somehow :D
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: sorting an array by word length

Post by dunbarx » Wed Aug 19, 2020 1:44 pm

Bogs.

Please get back to us when you settle down.

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: sorting an array by word length

Post by bogs » Wed Aug 19, 2020 1:54 pm

Ok, see you in 2030 then, take care :mrgreen:
Image

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”