How to sort similar variables together?

For example i have in my struct ->eg: test[0].blabla= MA for first record,test[1].blabla SC for 2nd, MA for 3rd, 4th MA, 5th GE, 6th SC, I can't seem to figure how to sort them according to MA,MA,MA,SC,SC,GE.

I think i need another struct array to temporarily save all the records into it, but i've been thinking for hours and i cant seem to figure out a code to do this sort.

I'm revolving around while loops,for loops,strcmp and strcpy. I would definitely appreciate it if anyone could help me on this with a rough idea or a rough code. Thanks alot!
Last edited on
Have you tried some sorting algorithms?
http://xoax.net/comp/alg/general/index.php

I think this can be done in a similar way, although they are strings (or I'm misunderstanding, that in fact they are some kind of objects?).

[edited]
Yes in this way you probably need a temporary save of a record when exchanging the elements, but not necessarily all of them.
Last edited on
Can I take it test is an array of std::string? If so, you can use std::sort to order your array.
sorry guys, but im still quite a beginner in c++, so i do not know std::sort and so on. my knowledge now is summarised to while,for,strcmp, and strcpy,if else and the very basics.

and yea, i jus wanna compare the letters XX only , simple as that. But just like wmheric says, is it the possible if i use bubble sort for a sort on grouping similar characters together, rather than the usual increasing or decreasing order of numerical or alphabetical characters?

I can't figure out a For loop or a While loop to do this checks. My approach currently is.. compare first record in duplicate struct with the first record in main struct with a For loop, and den save into the first record position of duplicate struct if its the same, and then i increment my counter and then continue searching thru main struct for similar XX and den save into duplicate struct, and then increment my counter again.

But i find myself 'expiring' the for loop as the For loop counter x reaches the condition num of example x<num where num is the number of records in the file. because in this way i only manage to group one type of XX together, the others are still left out and the loop has expired, thus cannot compare already..

I'm sorry guys of the confusing explanation, but my code is only halfway done, as im stumped. Thanks if anyone can help me on this?
Take a look at this:
http://cplusplus.com/forum/articles/10879/

The purpose of that article was to show that some algorithms such as count, fill, sort, copy, etc. are as simple as making a function call. If you know how to call a function then you already know how to use some of the std algorithms such as sort.

Using std::sort is as simple as this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>

int main()
{
    const int SIZE(5);
    int MyArray[SIZE] = { 5, 4, 1, 3, 2 };
    // the array name is a pointer to the first element and adding SIZE to it 
    // yields the pointer to one past the end of the array so that the sort knows 
    // where the end of the array is.
    std::sort(MyArray, MyArray + SIZE); // sorts ascending by default
    for(int i = 0; i < SIZE; ++i)
	std::cout << MyArray[i];
    std::cout << std::endl;
}

Give me a minute and I will show you an example using a predicate which is probably what you want to do. In the previous link that I posted there is a sorting example where the operator< function is defined for a class which allows sort to be used for arrays of class instances. Using std::sort is actually very simple, even for a beginner.
Last edited on
Actually, before I post any further example please tell us if you are writing C or C++ code. I'd like to assume C++ since this is a C++ website but that is not always the case and you seem to want to use C run time functions such as strcmp. it seems to me that you are performing a very simply sort of structures by organizing them in terms of a character array attribute. I think that you can define a simple predicate for use with the std::sort algorithm or you could define operator< for the struct.
I can't seem to figure how to sort them according to MA,MA,MA,SC,SC,GE.


Actually, neither can I. that doesn't look like a logical sort to me. It isn't alphabetical. What is the sort criteria?


Here is an example on sorting struct objects by their string attribute.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>

struct RecordsType
{
    std::string state;
    int someValue;
};

struct SortRecordsAscending
{
    bool operator()(const RecordsType& lhs, const RecordsType& rhs)
    {
	return (lhs.state < rhs.state);
	// if null terminated array
	//return (strcmp(lhs.str, rhs.str) < 0);
    }
};

struct SortRecordsDescending
{
    bool operator()(const RecordsType& lhs, const RecordsType& rhs)
    {
	return (rhs.state < lhs.state);
	
	// if null terminated array
	//return (strcmp(lhs.str, rhs.str) > 0);
    }
};

void SortingObjectsExample()
{
    const int SIZE(5);
    RecordsType MyArray[5] =
	{
	    { "MA", 5}, { "PA", 1 }, { "GE", 3 }, { "SC", 4 }, { "MA", 2 }
	};
    // notice that the SortRecordsAsending is being constructed.  The third arg
    // is an instance of the SortRecordsAsending struct.  Its operator()
    // is invoked as sort tries to determine the order of the objects in the
    // array.
    std::sort(MyArray, MyArray + SIZE, SortRecordsAscending());
    for(int i = 0; i < SIZE; ++i)
	std::cout << MyArray[i].state << ' ';
    std::cout << std::endl;

    // shuffle 
    std::random_shuffle(MyArray, MyArray + SIZE);
    for(int i = 0; i < SIZE; ++i)
	std::cout << MyArray[i].state << ' ';
    std::cout << std::endl;

    // sort descending.  
    std::sort(MyArray, MyArray + SIZE, SortRecordsDescending());
    for(int i = 0; i < SIZE; ++i)
	std::cout << MyArray[i].state << ' ';
    std::cout << std::endl;
}

int main()
{
    const int SIZE(5);
    int MyArray[SIZE] = { 5, 4, 1, 3, 2 };
    std::sort(MyArray, MyArray + SIZE); // sorts ascending by default
    for(int i = 0; i < SIZE; ++i)
	std::cout << MyArray[i];
    std::cout << std::endl;

    SortingObjectsExample();
}
hey kempofighter, really many thanks for your help, but i realised the sort was actually quite easy, it could have been done in same way as the bubble or exchange sort, i forgot which one. i was trying to sort in SIMILAR characters, rather than alphabetical, which would have been easier( which i have done so already.) but in any case i have to say thanks dedicating ur time to helping me! :)
Topic archived. No new replies allowed.