Sorting search results - solution needed

Niggemann, Bernd Bernd.Niggemann at uni-wh.de
Wed Oct 24 16:27:41 EDT 2018


here is my take, clumsy and not respecting all formatting requirements

Instead of

187.1
187.1.1
187.1.2
187.3
187.22
187.33
187.234

it sorts

187.1
187.1.1
187.1.2
187.22
187.234
187.3
187.33

Kind regards
Bernd




-----------------------------------------------
on mouseUp
   put field "fAlpha" into tData
   put "" into msg
   sort tData numeric by specialSort(each) 
   put tData into field "fSort"
end mouseUp

function specialSort pEach
   put pEach into tData
   --put word 1 to -1 of tData into tData
   put "0,1,2,3,4,5,6,7,8,9,." into tNumbers
   
   -- check for double colon like in 187.1.2
   put 0 into tSkip
   put 0 into tCounter
   repeat
      put offset(".", tData, tSkip) into tOffset
      if tOffset > 0 then
         add tOffset to tSkip
         add 1 to tCounter
      else
         exit repeat
      end if
   end repeat
   if tCounter > 1 then 
      --put tData && tCounter & cr after msg
      put empty into char tSkip of tData
      return tData
   end if
   
   repeat with i = 1 to the number of chars of tData
      if char i of tData is among the items of tNumbers then 
         put char i of tData after tNumberPart
      else
         exit repeat
      end if
   end repeat
   
   
   if tData = tNumberPart then
      return tNumberPart
   end if
   
   put length(tNumberPart) into tLength
   put char tLength + 1 to - 1 of tData into tRest
   if char tLength of tNumberPart = "." then put "." before tRest
   
   replace ")" with empty in tRest
   replace space with empty in tRest
   
   put toUpper(tRest) into tRest
   
   put true into tLeftBracket
   put 0 into tDecimal
   repeat with i = 1 to the number of chars of tRest
      
      put char i of tRest into tChar
      
      if tChar is a Number then
         add  tChar * .00001 to tDecimal
         
      else if tChar is "(" then
         if tLeftBracket then
            add .0000001 to tDecimal
            put false into tLeftBracket
         end if
         
      else if tChar is "." then -- ASCII "." = 46
         add .00001 to tDecimal
         
      else if charToNum(tChar) >= 65 then -- ASCII "A" = 65
         add (charToNum(tChar)-64) * .00009 to tDecimal
      end if
   end repeat
   
   return tNumberPart + tDecimal
end specialSort
-----------------------------------------------------




More information about the use-livecode mailing list