I need to sort a list of numbers

Here is the sample input: code -> subcode -> brandcode ( each code has a subcode and each subcode has a brandcode)

7366->65535->65535
7379->65535->65535
7379->65535->65535
7379->65535->65535
7366->65535->65535
7366->65535->65535
7379->65535->65535
7366->65535->65535
9113->65535->65535
7379->65535->65535
7315->5->65535
7314->65535->65535
7315->5->65535
7314->65535->65535
7379->65535->65535
7314->65535->65535
7314->65535->65535
7314->65535->65535
7311->65535->8
7314->65535->65535
7314->65535->65535
7314->65535->65535
7314->65535->65535
7314->65535->65535
7315->20->65535
9126->65535->299
7380->65535->65535
7311->65535->35
9126->65535->269
7379->65535->65535
7315->20->65535
7379->65535->65535
7379->65535->65535
7323->65535->65535
7379->65535->65535
7379->65535->65535
7379->65535->65535
7366->65535->65535
7379->65535->65535
7379->65535->65535
7379->65535->65535
7314->65535->65535
7315->5->65535
7379->65535->65535
7379->65535->65535
7314->65535->65535
7311->65535->35
7366->65535->65535
7314->65535->65535
7315->5->65535
7323->65535->65535
7379->65535->65535
7314->65535->65535
7315->5->65535
7315->5->65535
7314->65535->65535
7379->65535->65535
7379->65535->65535
7379->65535->65535
7314->65535->65535
7315->29->65535
7314->65535->65535
7315->29->65535
7314->65535->65535
7379->65535->65535
7379->65535->65535
7311->65535->8
7379->65535->65535
7379->65535->65535
7328->65535->1005
7366->65535->65535
7379->65535->65535
7379->65535->65535
7379->65535->65535
9126->65535->276
7315->29->65535
7317->65535->65535
7379->65535->65535
7377->65535->65535
7311->65535->8
7317->65535->65535
7328->65535->1005
7379->65535->65535
7379->65535->65535
7379->65535->65535
7311->65535->35
7311->65535->990
7311->65535->8
7311->65535->9
7370->65535->65535
7311->65535->8
7315->29->65535
7314->65535->65535
9122->65535->65535
7311->65535->35
7311->65535->990
7311->65535->8
7321->65535->65535
7311->65535->82
7311->65535->10
7311->65535->9
7311->65535->11
7311->65535->82
7311->65535->35
7311->65535->10
7311->65535->10
7311->65535->11
7311->65535->35
7311->65535->9
7311->65535->10
7311->65535->10
7311->65535->9
7311->65535->8
7311->65535->82
7311->65535->9
7314->65535->65535
7311->65535->11
7311->65535->11
7311->65535->10

Sorting should be done in such a way that code is taken as primary key, subcode as secondary, brandcode as third key.

Hope anyone can help me with this.

Thanks
jazz

convert the strings to a have fixed-length fields

7379->65535->65535
7315->    5->65535

The the above, just to a simple comparison

no no, they are not strings, they are 3 fields. I gave them in that way to avoid confusion.

jazz

I'd use qsort .

#include <stdio.h>
#include <stdlib.h>

struct type
{
   int code, subcode, branch;
};

int compare(const void *a, const void *b)
{
   const struct type *x = a, *y = b;
   if ( x->code < y->code )
   {
      return -1;
   }
   if ( x->code > y->code )
   {
      return 1;
   }
   if ( x->subcode < y->subcode )
   {
      return -1;
   }
   if ( x->subcode > y->subcode )
   {
      return 1;
   }
   if ( x->branch < y->branch )
   {
      return -1;
   }
   if ( x->branch > y->branch )
   {
      return 1;
   }
   return 0;
}

struct type Array[] =
{
   {7366,65535,65535},
   /* ... */
   {7311,65535,10},
};

int main(void)
{
   /* ... */
   qsort(Array, sizeof Array / sizeof *Array, sizeof *Array, compare);
   /* ... */
   return 0;
}

struct type Array[] =
{
{7366,65535,65535},
/* ... */
{7311,65535,10},
};

How is it possible to enter all the sets into the array, is'nt it not time consuming???

jazz

I just used my favorite editor which has a regex search and replace.

struct type Array[] =
{
   {7366,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7366,65535,65535},
   {7366,65535,65535},
   {7379,65535,65535},
   {7366,65535,65535},
   {9113,65535,65535},
   {7379,65535,65535},
   {7315,5,65535},
   {7314,65535,65535},
   {7315,5,65535},
   {7314,65535,65535},
   {7379,65535,65535},
   {7314,65535,65535},
   {7314,65535,65535},
   {7314,65535,65535},
   {7311,65535,8},
   {7314,65535,65535},
   {7314,65535,65535},
   {7314,65535,65535},
   {7314,65535,65535},
   {7314,65535,65535},
   {7315,20,65535},
   {9126,65535,299},
   {7380,65535,65535},
   {7311,65535,35},
   {9126,65535,269},
   {7379,65535,65535},
   {7315,20,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7323,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7366,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7314,65535,65535},
   {7315,5,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7314,65535,65535},
   {7311,65535,35},
   {7366,65535,65535},
   {7314,65535,65535},
   {7315,5,65535},
   {7323,65535,65535},
   {7379,65535,65535},
   {7314,65535,65535},
   {7315,5,65535},
   {7315,5,65535},
   {7314,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7314,65535,65535},
   {7315,29,65535},
   {7314,65535,65535},
   {7315,29,65535},
   {7314,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7311,65535,8},
   {7379,65535,65535},
   {7379,65535,65535},
   {7328,65535,1005},
   {7366,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {9126,65535,276},
   {7315,29,65535},
   {7317,65535,65535},
   {7379,65535,65535},
   {7377,65535,65535},
   {7311,65535,8},
   {7317,65535,65535},
   {7328,65535,1005},
   {7379,65535,65535},
   {7379,65535,65535},
   {7379,65535,65535},
   {7311,65535,35},
   {7311,65535,990},
   {7311,65535,8},
   {7311,65535,9},
   {7370,65535,65535},
   {7311,65535,8},
   {7315,29,65535},
   {7314,65535,65535},
   {9122,65535,65535},
   {7311,65535,35},
   {7311,65535,990},
   {7311,65535,8},
   {7321,65535,65535},
   {7311,65535,82},
   {7311,65535,10},
   {7311,65535,9},
   {7311,65535,11},
   {7311,65535,82},
   {7311,65535,35},
   {7311,65535,10},
   {7311,65535,10},
   {7311,65535,11},
   {7311,65535,35},
   {7311,65535,9},
   {7311,65535,10},
   {7311,65535,10},
   {7311,65535,9},
   {7311,65535,8},
   {7311,65535,82},
   {7311,65535,9},
   {7314,65535,65535},
   {7311,65535,11},
   {7311,65535,11},
   {7311,65535,10},
};

I copied what you posted, pasted, and did a couple of search and replace ops like -> to , or \n to },\n .

hmm.... any other way apart from that, like storing the list in a file, and then reading from the file, sorting it and then writing back??? because if the list is very very lengthy, i guess entering a long long list into the code doesnt look good.

jazz

hmm.... any other way apart from that??? because if the list is very very lengthy, i guess entering a long long list into the code doesnt look good.

Are you beating around the bush trying to say that your information comes from a formatted file?

Given that you now know a way to sort, can we assume that you know how to open a file and extract data? And you can make a basic attempt at putting 2 and 2 together? Or are you asking how to read the data from a file?

[edit]Is there any particular language you're supposed to use (C or C++)?

Oh god, dont be so furious.
Anyways thanx for ur help.

jazz

No, I'm just asking questions.

Is the information contained in a formatted file?
Do you know how to read and parse the data from the file?
Do you need to use dynamic allocation?
Are you using C or C++?

Different responses to these questions would result in different answers, different code. And you haven't posted any code yet, so if you are expecting more help, it would behoove you to start posting some attempts.

The code related to my question is posted under the topic "help in the program",
the fact is iam very new to programing, in fact just started my career, so facing lil bit problems.... Thankx for understanding.

jazz

Ah. Sometimes it helps to keep related stuff together in the same thread.

Taking a look at that code, one question comes to mind: Why all the casting? If your answer is "to shut off warnings/errors", then you are headed down a difficult path in your coding career.

Also, try to indent consistently, I have not looked much at this code because it's, well, not pretty.

Ah, that is the code written by someone else, and now iam given a task to understand it and then modify it, so that it sorts even by subcode and brand code.

I surely see that iam headed down a difficult path in my coding career. :)

I'd say the person who wrote that code is well on his way down the hard path. ;)

Member Avatar for iamthwee

I'd say the person who wrote that code is well on his way down the hard path. ;)

...or hanging :lol:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.