Sorting glitch

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
Maxiogee
Posts: 38
Joined: Thu May 05, 2011 5:45 pm

Sorting glitch

Post by Maxiogee » Mon Oct 06, 2014 5:13 pm

I have a field (named "Personnel") which contains lines of names of people appearing on music albums.
Following each name is a list of the things the person is credited with.

The primary item delimiter is "•", and then the person's entries are separated by commas

The Personnel field on one card is this

Jefferson Airplane • Primary Artist
Grace Slick • Keyboards, Organ, Piano, Primary Artist, Recorder, Vocals, Composer
Jack Casady • Fuzz Bass, Primary Artist, Rhythm Guitar, Bass Guitar
Jorma Kaukonen • Guitar (Rhythm), Lead Guitar, Primary Artist, Vocals, Composer
Marty Balin • Album Design, Guitar, Primary Artist, Vocals, Composer
Paul Kantner • Primary Artist, Rhythm Guitar, Vocals, Composer
Spencer Dryden • Percussion, Primary Artist, Drums

Additional personnel
Darby Slick • Composer
David Hassinger • Engineering
Herb Greene • Photography
Jerry Garcia • Guitar
Rick Jarrard • Producer
Skip Spence • Composer
- - - - - END - - - - -

I fill this field when I have copied the album's data from a variety of sources and edited out the duplication in a text-editor.

The field-filling script is as follows

Code: Select all

On mouseUp
   if "- - - - - END - - - - -" is not in field "Personnel" then
      put the clipboarddata into field "Personnel"
      
      put lineoffset ("Additional personnel", field "Personnel") into Numn
      put line 2 to (numn-2) of field "Personnel" into fred
      sort lines of fred
      put fred into line 2 to (numn-2) of field "Personnel"
      
      put line (numn+1) to -1 of field "Personnel" into fred
      sort lines of fred
      
      put fred into line (numn+1) to -1 of field "Personnel"
      put  "- - - - - END - - - - -" after field "Personnel"
      put 0 into maxer
      repeat until "[" is not in field "Personnel"
         put offset ("[", field "Personnel") into Sta
         put offset ("]", field "Personnel") into Sto
         delete char sta to sto of field "Personnel"
      end repeat
      
      repeat with z = number of chars in field "Personnel" down to 1
         if char z of field "Personnel" = " " and char (z-1) of field "Personnel" = " " then delete char z of field "Personnel"
      end repeat
      repeat with b = 1 to number of lines in field "Personnel"
         set itemdelimiter to "•"
         if "•" is in line b of field "Personnel" then
            put item 2 of line b of field "Personnel" into fred
            if last char of fred = " " then delete last char of fred
            set itemdelimiter to ","
            sort items of fred ascending
            set itemdelimiter to "•"
            put fred into item 2 of line b of field "Personnel"
         end if
         repeat with y = 1 to number of words in item 2 of line b of field "Personnel"
            if the chartonum of Char 1 of word y of item 2 of line b of field "Personnel"  > 96 and the chartonum of Char 1 of word y of item 2 of line b of field "Personnel"  < 123 then
               put the numtochar of ( the chartonum of Char 1 of word y of item 2 of line b of field "Personnel"  - 32) into Char 1 of word y of item 2 of line b of field "Personnel" 
            end if
         end repeat
      end repeat
      
      save this stack
   end if
end mouseUp
The field listed above has been sorted, but the entires are not in the correct order. There is an out-of-sequence entry for each person in the band-member section.
What am I doing wrong?

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

Re: Sorting glitch

Post by dunbarx » Mon Oct 06, 2014 6:17 pm

Hi.

Tricky. But look at the items. They all start with a space. Hint, hint.

Do not look at the following until you get what I meant.

Cheater.

Think about this:

sort items of yourData ascending by char 2 to 100 of each.

Now, since you looked, tell us another way to fix this. It will not be as elegant, but that does not matter here.

Craig Newman

Maxiogee
Posts: 38
Joined: Thu May 05, 2011 5:45 pm

Re: Sorting glitch

Post by Maxiogee » Mon Oct 06, 2014 6:29 pm

Why does the space matter, Craig?

Looks like I'll have to do a stripping out of the spaces following comas and then sort and then re-insert them.

If only I could use a doub;e-character itemdelimiter - ", "!!

Maxiogee
Posts: 38
Joined: Thu May 05, 2011 5:45 pm

Re: Sorting glitch

Post by Maxiogee » Mon Oct 06, 2014 6:37 pm

I've settled for adapting the sort as follows.

Code: Select all

 
repeat with c = 1 to number of items in fred
   if char 1 of item c of fred = " " then delete char 1 of item c of fred
 end repeat
 sort items of fred ascending
 repeat with c = 1 to number of items in fred
   put " " before char 1 of item c of fred
 end repeat
     
Thanks, Craig.

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

Re: Sorting glitch

Post by dunbarx » Mon Oct 06, 2014 8:59 pm

Hi,
Why does the space matter, Craig?
Why none at all, now that I think of it, instead of just winging it. And if i had read that you were the OP, I would not have beens so coy.

Surely spaces in a string of items would be sorted by their ASCII value, and then all the subsequent chars as well. When I saw the space in front of each item, I stopped thinking, deleted them, saw that the sort worked, and wrote back.

I put your data into a field and tried this:

Code: Select all

on mouseUp
   get fld 1
   repeat with y = 1 to the number of chars in it
      put  char y of it && charTonum(char y of it) into line y of accum
       if charTonum(char y of it) = 202 then put "**"  after line y of accum
   end repeat
end mouseUp
Where did ASCII 202 come from? The answer, of course, is in the source data. I get fouled up by this all the time. Perhaps this is a clue? Certainly the sort command works.

Craig

Maxiogee
Posts: 38
Joined: Thu May 05, 2011 5:45 pm

Re: Sorting glitch

Post by Maxiogee » Tue Oct 07, 2014 10:03 am

Yes, I discovered that deleting the spaces did not work because it did not delete all the spaces, because I only asked it to look for " ". I had to copy the offending 'space' into the script.

Thanks for the insight.

Post Reply