swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Qsort


From: Matteo Morini
Subject: Re: Qsort
Date: Mon, 21 May 2001 13:44:13 +0200

    Hello everybody,

here is a sample of my code in which QSort is used (and works :-) ); only
relevant lines appear:

//--------------------
//Agent.m

[...]

- (int) compare: other
{
  if (self == other) {
    return 0;
  }
  if ([self getValue] > [other getValue]) {
    return 1;
  }

  if ([self getValue] == [other getValue]) {
    return 0;
  }

  return -1;
}

[...]

- (int) getValue
{
    return value;
}

//-------------

//AgentList.m

[...]

- sortAgentQueues
{
  [QSort sortObjectsIn: AgentQueue];
  return self;
}

[...]
//-------------

    I've implemented my "compare" method for class "Agent", which returns
-1, 0, 1 according to the ifs you can read above, in the "Agent" secion.
Method "getValue" returns an integer iVar (I can't see why it shouldn't
work with a float), according to which Agents will get ordered.
    In AgentList I've got "sortAgentQueues", the method that effectively
sorts the Agents (which at creating time have been put in AgentQueue in no
particular order).  That simple.

    Greetings, Matteo Morini

PS: Seems like us Objective-C Swarm(-ists? -ers?) are very few, by now...
:-)

address@hidden wrote:

> Hello
>
> I'm having problem with QSort. My aim is to sort a List of object
> accordingly to the value returned by one of the method of the objects.
> That is, I have a List * population, made by several calls to
> [population addLast: bug]. I'd like to sort bugs accortingly to (float)
> [bug getFitness]. I've tried QSort with [population setCompareFunction],
> but at best I get a segmentation fault. Could you please post some code
> where I see how to use QSort.
>
> thanks
> marcello
>
> p.s. i've looked at QSortTest.m but it's no use since there it sorts a
> (collection ) vector of numbers.
> --
> Marcello Gallucci (Ph.D)
>
> Department of Social Psychology
> Free University
> Van der Boechorststraat 1
> 1081 BT Amsterdam (NL)
> Tel. +31(0)20 4448846
>


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]