Thread: Sorting a struct array(numerical+alphabetical)

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    3,874
    Quote Originally Posted by emano12 View Post
    Thank you for the response. How could I solve it? Tried with "vara" but didnt work.
    I suggest you remove the words "didn't work" from any more posts. I hate playing 20 questions.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #17
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    very helpfull, been trying for a while with that. No matter what i change it still keeps on crashing. And because im fairly new, i cant figure out why because i get no errors.

  3. #18
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    What do you have problems with. The signature of qsort is this:
    void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));

    First param: first elem of the array => items or &items[0]
    Second param: number of array elements => NUM_ITEMS
    Third param: size of an array element => sizeof(items[0] or sizeof(struct varam)
    Forth param: the compare function => nummer_sortering

  4. #19
    Registered User
    Join Date
    Jan 2018
    Posts
    18

    Finally

    Quote Originally Posted by OldGuy2 View Post
    What do you have problems with. The signature of qsort is this:
    void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));

    First param: first elem of the array => items or &items[0]
    Second param: number of array elements => NUM_ITEMS
    Third param: size of an array element => sizeof(items[0] or sizeof(struct varam)
    Forth param: the compare function => nummer_sortering
    It was the third param that I had problems with! Thank you

  5. #20
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    When printing the sorted elements they get sorted, but the diffrent elements get mixed up and wrong.

    They should be printed like this:

    nummer: 1575
    namn: Anchor Steam Beer
    pris: 22.900000
    volym: 355.000000
    typ: Mellanm├rk lager
    stil: California common
    forpackning: Flaska
    land: USA
    producent: Anchor Brewing Company
    alkoholhalt: 4.800000



    but get printed all messed up like this :

    pris: 0.000000
    volym: 6099781802703388100000000000000000.000000
    typ:
    stil:
    forpackning:
    land: @
    producent: 
    alkoholhalt: 0.000000

    I guess I am missing something

    code:

    Code:
    int nummer_sortering(const void* n1, const void* n2)
    			{
    
    
    				const struct vara *item1 = n1;
    				const struct vara *item2 = n2;
    
    
    				return item1->nummer - item2->nummer;
    			}
    
    
    			printf("\n\nVaror sorterade p varunummer:\n");
    			qsort(items, NUM_ITEMS, sizeof(items[0]), nummer_sortering);
    			for (i = 0; i < NUM_ITEMS; i++)
    			{
    					
    					printf("nummer: %d\n"
    						"namn: %s\n"
    						"pris: %f\n"
    						"volym: %f\n"
    						"typ: %s\n"
    						"stil: %s\n"
    						"forpackning: %s\n"
    						"land: %s\n"
    						"producent: %s\n"
    						"alkoholhalt: %f\n\n",
    						items[i].nummer,
    						items[i].namn,
    						items[i].pris,
    						items[i].volym,
    						items[i].typ,
    						items[i].stil,
    						items[i].forpackning,
    						items[i].land,
    						items[i].producent,
    						items[i].alkoholhalt
    					);
    
    
    
    
    					free(oneline);
    				
    			}
    		}
    		
    
    
    		fclose(fp);
    	}

  6. #21
    Registered User
    Join Date
    May 2009
    Posts
    3,874
    As I stated before NUM_ITEMS is likely wrong!

    You only want to sort the valid data in the array; do not sort the empty/garbage elements.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #22
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    As I stated before NUM_ITEMS is likely wrong!

    You only want to sort the valid data in the array; do not sort the empty/garbage elements.

    Tim S.
    Could you explain more?

    I have : #define NUM_ITEMS sizeof(items)/sizeof(items[0]) for the moment.
    Last edited by emano12; 01-30-2018 at 06:22 AM.

  8. #23
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    I think you misunderstood Tim.

    The calculation of NUM_ITEMS is correct: the size of the entire array divided by the size of a single element.

    What Tim is saying is that NUM_ITEMS is not the thing you should pass to qsort (unless, of course, your array is completely full).

    You have an array that can hold 100 items. But if you only happen to have 42 valid entries in your array, the other 58 entries are still there, they're just full of garbage. When you sort, those garbage items get mixed in with your real data, which is likely what causes your bogus output.

    What you need is some sort of variable to track how many valid items are in your array, and only sort that many. if only you had a variable that counted the number of items you read in from your file.

  9. #24
    Registered User
    Join Date
    Jan 2018
    Posts
    18

    Thank you

    Quote Originally Posted by anduril462 View Post
    I think you misunderstood Tim.

    The calculation of NUM_ITEMS is correct: the size of the entire array divided by the size of a single element.

    What Tim is saying is that NUM_ITEMS is not the thing you should pass to qsort (unless, of course, your array is completely full).

    You have an array that can hold 100 items. But if you only happen to have 42 valid entries in your array, the other 58 entries are still there, they're just full of garbage. When you sort, those garbage items get mixed in with your real data, which is likely what causes your bogus output.

    What you need is some sort of variable to track how many valid items are in your array, and only sort that many. if only you had a variable that counted the number of items you read in from your file.
    Here is the link to my question!(c - qsorting a struct with arrays, error as output - Stack Overflow) I guess you are saying that I have the variable in my code aldready haha, Can you give me a clue? I am really bad at this sorry
    Last edited by emano12; 01-31-2018 at 07:09 AM. Reason: link

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I wonder if that bolded i was a clue... if only there was some clue about the clue! Oh no!

  11. #26
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    I tried "i" haha, but it didnt work out. Sneaky though

  12. #27
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    I do not understand why the output is all messy. I have explained the question better on Stackoverflow
    c - qsorting a struct with arrays, error as output - Stack Overflow

  13. #28
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    What does that mean, it didn't work out? If it didn't work out you make it work out. Maybe make listanr() return its i, and then use that number for this purpose.

    No reason to just deny the answer staring us right in our faces.

  14. #29
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    I did not mean it in a cruel way. Im so sorry! I am just a little frustrated with programming, because I have not got the hang of it. I really appreciate you helping me, do not think otherwise. Could you be an angel and check out the link? Then you will understand my "anger" about the output

  15. #30
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Could you be an angel and check out the link?
    I did, the link is as bad as the explanation here.

    Frankly, no one can compile your code, you've neglected to include an entire program at any point. Context that you've deemed unimportant is absent, like where main() is, what it contains, and even how many functions there might be. Thus, it is difficult to even explain things that we see at a glance. It's unsurprising that "i" didn't work when you tried it because I'm sure the actual code looks waaaaay different from what you thought you should post.

    Read this for next time: How to create a Minimal, Complete, and Verifiable example - Help Center - Stack Overflow

    At this point, starting over with a good example is as troublesome as not doing that, so I'll just tell you.


    Code:
     for (i = 0; i < 100 && fgets(envara, 512, fp); i++)
    Do you remember where this loop is in your code? Go find it. Well, when this loop finishes, you know exactly how many beers are in the array. It's i. You need to save, and use, this number for sorting. Whatever else that might entail, like returning it from one function and passing it to several others, well, you've made that your problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array of struct inside struct
    By blueboyz in forum C Programming
    Replies: 13
    Last Post: 04-24-2012, 02:15 AM
  2. Alphabetical sorting in a character array?
    By YPavluk in forum C Programming
    Replies: 10
    Last Post: 10-28-2011, 08:35 AM
  3. C++ Alphabetical sorting, etc Help
    By Lucifix in forum C Programming
    Replies: 5
    Last Post: 05-17-2010, 01:29 AM
  4. Alphabetical sorting function
    By typer in forum C Programming
    Replies: 6
    Last Post: 05-20-2006, 02:35 AM
  5. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM

Tags for this Thread