Sorting String Help

Game Editor comments and discussion.

Sorting String Help

Postby Bee-Ant » Sun Feb 17, 2008 4:30 am

You have to sort some string data which consist of 3 elements depend on their name...for example
Code: Select all
=> How many your data ? 3

=> 1. Name : G
         Class : 3
         Color : Red
=> 2. Name : Y
         Class : 2
         Color : Blue
=> 3. Name : D
         Class : 2
         Color : White

=> Output
=> 1. Name : D
         Class : 2
         Color : White
=> 2. Name : G
         Class : 3
         Color : Red
=> 3. Name : Y
         Class : 2
         Color : Blue

I know how to sort SINGLE string...but at this, I'm still confused
Thanks for your help :D
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: Sorting String Help

Postby edh » Mon Mar 03, 2008 4:16 pm

You would typically have to compare each element in some order. I would recommend making a function for this.

So, if the struct is called Object and the comparison order is Name, Class, Color, you would compare it like this:

Code: Select all
int objectCompare(Object *o1, Object *o2)
{
  int result = strcmp(o1->name, o2->name);
  if (result == 0)
  {
    result = strcmp(o1->color, o2->color);
    if (result == 0)
    {
      result = strcmp(o1->class, o2->class);
    }
  }
  return result;
}
User avatar
edh
 
Posts: 233
Joined: Thu Sep 13, 2007 12:17 am
Location: Maine, USA
Score: 14 Give a positive score

Re: Sorting String Help

Postby Bee-Ant » Tue Mar 04, 2008 5:48 am

Thanks edh :D , but it's really late... :?
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: Sorting String Help

Postby edh » Tue Mar 04, 2008 11:01 am

I really don't like to see someone with a question go unanswered. I hope you either figured it out in the past two weeks, or it helped.
User avatar
edh
 
Posts: 233
Joined: Thu Sep 13, 2007 12:17 am
Location: Maine, USA
Score: 14 Give a positive score

Re: Sorting String Help

Postby Bee-Ant » Tue Mar 04, 2008 11:15 am

Oops, sorry if I had offended you..no purpose on that...Errr ehm, OK...I'll save your code for my next homework...
Thanks :D :oops:
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron