0

Suppose I have 4 integers.

int a = 4;
int b = 2;
int c = 4;
int d = 1;

How can I sort these integers from smallest to biggest. The output needs to be something like this: d, b, a, c Most methods of sorting only give me the value of the sorted integer. I need to know the name.

Edit: Well, I'm writing an AI algorithm. I have 4 ints that store direction priority. (If the AI comes into a wall, it chooses the next best direction). So, I need to find the lowest int, and if the AI can't move that way, I choose the second to lowest etc.

Sosumi
  • 759
  • 6
  • 20
  • It feels like you're approaching the problem in the wrong way (getting the variable name seems useless, you already have the value), maybe giving more details on the problem you are trying to solve would help. – Nick Jan 24 '12 at 00:22
  • put it to [NSArray and sort](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5) with custom NSSortDescriptor – dive Jan 24 '12 at 00:24
  • What have you tried? What kind of data structure are you thinking of to hold these numbers? – Caleb Jan 24 '12 at 00:32

3 Answers3

4

There appears to be some confusion here; in your example a is not the "name" for the value 4, it is the name of an integer variable which currently contains 4. In other words "a" is not part of the data of your program.

What I assume you mean is you have name/value pairs which you wish to sort using the value as key. A common way to do this is to define a type for your pair, create a collection, and sort the collection.

In plain C you can declare:

typedef struct
{
   char *name;
   int value;
} MyPair;

You can create an array of these and sort it using standard C functions for array sorting, using just the value field as the key.

In Objective-C you can declare a class for your pair:

@interface MyPair : NSObject
{
   NSString *name;
   int value;
}

// methods/properties

@end

You can create an NSMutableArray of instances of MyPair and then sort the array, again you just use the value property (or instance variable) when doing the comparisons for the sort algorithm.

There are other variations of course. Once sorted you can iterate through the sorted array and display the name field/property.

CRD
  • 52,522
  • 5
  • 70
  • 86
2

Here is an objective-c approach. Unfortunately, you will not have the fun of writing the AI portion, the sorting is built into the libraries already.

int north = 1, south = 3, east = 2, west =4;

  NSDictionary * nDict = [NSDictionary dictionaryWithObjectsAndKeys:@"north", @"name", [NSNumber numberWithInt:north], @"value", nil];
  NSDictionary * sDict = [NSDictionary dictionaryWithObjectsAndKeys:@"south", @"name", [NSNumber numberWithInt:south], @"value", nil];
  NSDictionary * eDict = [NSDictionary dictionaryWithObjectsAndKeys:@"east", @"name", [NSNumber numberWithInt:east], @"value", nil];
  NSDictionary * wDict = [NSDictionary dictionaryWithObjectsAndKeys:@"west", @"name", [NSNumber numberWithInt:west], @"value", nil];


  NSArray * toBeSorted = [NSArray arrayWithObjects:nDict,sDict,eDict,wDict,nil];
  NSArray * sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"value" ascending:NO]];
  NSArray * sorted = [toBeSorted sortedArrayUsingDescriptors:sortDescriptors];
  NSLog(@"sorted %@", sorted);

Output

2012-01-23 19:50:21.079 TestEnvironment[19792:207] sorted (
        {
        name = west;
        value = 4;
    },
        {
        name = south;
        value = 3;
    },
        {
        name = east;
        value = 2;
    },
        {
        name = north;
        value = 1;
    }
)

Now you can check the highest priority by

NSString * highestPriority = [[sorted objectAtIndex:0] objectForKey:@"name"];

Now you have some classes you can look up (NSArray, NSDictionary, NSSortDescriptor, NSNumber)

Jesse Black
  • 7,966
  • 3
  • 34
  • 45
  • ugh, dictionary-based programming. do yourself a favor and make this a proper class. – Dave DeLong Jan 24 '12 at 01:03
  • @DaveDeLong I use proper classes when coding, but considering the OP started with ints, I wasn't about to jump into a 2-3 page solution. This exposes them to some of the basic more common classes in objC – Jesse Black Jan 24 '12 at 03:20
0

You've tagged this Objective-C, but you haven't written anything that suggests using Objective-C. If you want to use Objective-C, I'd put the elements into an NSMutableArray (they'll need to be converted to NSNumbers to do that), and have the array sort them, as seen here.

If you just want to put them into a straight C array, you could sort them using heapsort (), qsort(), or mergesort().

Community
  • 1
  • 1
user1118321
  • 25,567
  • 4
  • 55
  • 86