LUA question: Why doesn't my table.sort work ?

Discussion created by remkodobber on Nov 19, 2008
Latest reply on Nov 20, 2008 by carstein.seeberg
I have a script that gets a processlist from the processes probe,
and tries to sort it by cpu usage. The table.sort part doesn't work,
and I can't work out why. I got the table.sort function from

-- LUA script:

-- get nimid (for notename), hostname (robotname)
alist = alarm.get()
if alist == nil then
   note_name = "TESTNOTE" -- running stand alone
   processes = "processes"
else - triggered by an alarm
   note_name = "processes-"..alist.nimid
   processes = "/"..alist.domain.."/"..alist.hub.."/"..alist.robot.."/processes"

note_id = note.find (note_name)
if note_id == nil then
   note_id = note.create (note_name,"Initial Create at " ..timestamp.format())

if alist ~= nil then
   note.attach (note_id,alist.nimid)

note.append (note_id," ")
note.append (note_id,"***Process information from "..processes.." added at "..timestamp.format())

-- Get the process-list from the processes probe, show all cpu-consuming processes
plist = nimbus.request(processes,"list_processes")

if plist == nil then
   printf ("The 'processes' probe does not run on the robot '%s'.",processes)
   note.append (note_id,"Probe "..processes.." does not run ")
   -- sort the table by cpu usage - does not work
   table.sort(plist, function  (a,b)
       return (a.cpu_usage > b.cpu_usage)

   for pid,proc in pairs(plist) do
      if proc.cpu_usage == nil then
         printf ("Executable pid:%05d cpu value is non-existent !!!!",proc.process_id)
      elseif proc.cpu_usage > 0 then
         process = string.format("%05d, cpu: %0.2f%% - %s",proc.process_id,proc.cpu_usage,proc.short_executable)
         print (process)