Sorting search results - solution needed

Geoff Canyon gcanyon at gmail.com
Wed Oct 24 14:59:51 EDT 2018


I think this comes pretty close to the requirement. It breaks the lines
down into numeric/not numeric items, and then sorts from the least
significant (rightmost) to most significant items. For each sort, sorts
first by alpha, then numeric. That results in letters being sorted before
numbers, which seemed wrong for this application, so I added the
returnOnlyNumbers function that (nearly) guarantees letters sorting after
numbers.

If the parenthesis etc. gets in the way, then removing them from the sort
could be done by building an array mapping the original list to the sort
keys, and then sorting on a function that returns the appropriate sort key.
But for now this seems pretty close.

function sortByAlternateNumericItems lineList,D
   -- takes a line list and a delimiter D that must not be in the lineList
   -- breaks each line down into numeric/non-numeric items
   -- then sorts by the items, from least significant (rightmost) to most
significant.
   -- for each sort, sorts first by alpha, then numeric, so numbers sort
before letters

   if D is in lineList then
      answer warning D && "is in the list" with cancel
      exit to top
   end if

   put 0 into maxItems
   repeat for each line L in lineList
      put numericItems(L,D) into newL
      get the number of items of newL
      if it > maxItems then put it into maxItems
      put newL & cr after R
   end repeat
   delete char -1 of R
   set the itemDel to D
   repeat with i = maxItems down to 1
      sort lines of R by item i of each
      sort lines of R numeric by returnOnlyNumbers(item i of each)
   end repeat
   replace D with empty in R
   return R
end sortByAlternateNumericItems

function returnOnlyNumbers N
   if N is a number then return N else return 999999999999999
end returnOnlyNumbers

function numericItems S,D
   -- returns the string S broken into number/not-number items, delimited
by D
   -- D must be chosen so that it does not occur in S
   if D is in S then
      answer warning D && "is in" && S with cancel
      exit to top
   end if
   put char 1 of S is a number into isNum
   put 1 into B
   repeat with E = 2 to length(S)
      if isNum then
         if (char B to E of S is a number) then next repeat
         put (char B to E - 1 of S) & D after R
         put E into B
         put false into isNum
      else if char B to E of S is a number then put true into isNum
      else
         repeat with NB = B + 1 to E
            if char NB to E of S is not a number then next repeat
            put (char B to NB - 1 of S) & D after R
            put NB into B
            put true into isNum
            exit repeat
         end repeat
      end if
   end repeat
   return R & char B to E of S
end numericItems

This sorts this list:

187
187.01
187.02
187.1
187.22
187.234
187.3
187.33
187.456
10187
18700
187 (a)
187 (b)
187 (b)(1)
187 (a)(1)
187.1.1
187.1.2
187.c
187a
187c
187 a
187 1
1
a
c
b

to this:

1
187
187a
187c
187 1
187 (a)
187 (a)(1)
187 (b)
187 (b)(1)
187 a
187.c
187.01
187.02
187.1.1
187.1.2
187.1
187.22
187.234
187.3
187.33
187.456
10187
18700
a
b
c



On Wed, Oct 24, 2018 at 9:08 AM Bob Sneidar via use-livecode <
use-livecode at lists.runrev.com> wrote:

> I should have said, in the loop keep adding a character until the value is
> no longer a number, then sort numeric by the last value.
>
> Bob S
>
>
> > On Oct 23, 2018, at 14:14 , Bob Sneidar via use-livecode <
> use-livecode at lists.runrev.com> wrote:
> >
> > This of course cannot be a numeric sort. I would create a loop and start
> with 1 digit, then see if the value div the value = the value (in a try
> catch loop). Once you get an error or false, break out the integer portion,
> pad it with the appropriate number of trailing zeros, then append the rest.
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list