Sorting objects

Hi , The code which i have written is to read the data from a file to the class objects bikes. Now my problem is , how can i sort them. Its like i want to sort it by name , or by ascending order of price , the same brands of bikes , sorting in ascending order of power like that. So how can i sort the objects??

Please help me with this , i tried with bubble sort using objects as implied to a function called sorting with arguements as the array of objects and choice of sorting like , sorting in terms of power , name , price. I took the choice and used it with switch(choice) , but i didnt get the result.



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
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdio.h>
using namespace std;
int size=2;
class listing
{
public:
    string name;
	string price;
	string brand;
	string displacement;
	string power;
	string category;
};
int main()
{
	int i;
	listing bikes[10];
	string input;
	ifstream datafile;
	datafile.open("Bikes.txt",ios::in);
	if(datafile.is_open())
	{
        for(i=0;i<size;i++)
        {
            if(datafile.good())
            {
                getline(datafile,input,'$');
                bikes[i].name=input;
                getline(datafile,input,'$');
                bikes[i].price=input;
                getline(datafile,input,'$');
                bikes[i].brand = input;
                getline(datafile,input,'$');
                bikes[i].displacement = input;
                getline(datafile,input,'$');
                bikes[i].power = input;
                getline(datafile,input,'$');
                bikes[i].category = input;

                cout<<bikes[i].name<<"\n"<<bikes[i].price<<endl<<bikes[i].brand<<endl<<bikes[i].displacement<<endl<<bikes[i].power<<endl<<bikes[i].category<<endl;
            }
        }
    }
    else
    cout<<"File is not open!"<<endl;
}
Could you show us your sorting function?
1
2
3
4
5
6
7
8
9
10
template < Class t >

T Sort ( T A[] , Int n )
{
     for(i=0;i<n-1;i++)
     for(j=0;j<n-i-1;i++)
     if(bikes[j].name>bikes[j+1].name)
     {
         swap ( bikes[j],Bikes[j+1]
      }


Hope u got a crude idea of what i was trying to do?
Hi,

Add operator< function to your class(shown below).
Use updated Sort funtion for sorting the list objects.

Revised Code:

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
class listing
{
public:
	string name;
	string price;
	string brand;
	string displacement;
	string power;
	string category;

	bool operator <(const listing& i_RListing)
	{
		if(name!=i_RListing.name)
			return name<i_RListing.name;
		else if(price!=i_RListing.price)
			return price<i_RListing.price;
		else if(brand!=i_RListing.brand)
			return brand<i_RListing.brand;
		else if(displacement!=i_RListing.displacement)
			return displacement<i_RListing.displacement;
		else if(power!=i_RListing.power)
			return power<i_RListing.power;
		else 
			return category<i_RListing.category;
	}
};


template <class T>
void Sort (T A[] , int n )
{
	 for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
			if(!(A[i]<A[j]))			
				std::swap (A[i],A[j]);			
}

int main()
{
       //Example 
	listing arrListings[2];
	arrListings[0]=listing();
	arrListings[1]=listing();
	Sort(arrListings,2);  

    return 0 ;
}


Let me know if you have any concerns on the above code.
Last edited on
Ya i got it , Can u explain this one for me in simple words.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool operator <(const listing& i_RListing)
	{
		if(name!=i_RListing.name)
			return name<i_RListing.name;
		else if(price!=i_RListing.price)
			return price<i_RListing.price;
		else if(brand!=i_RListing.brand)
			return brand<i_RListing.brand;
		else if(displacement!=i_RListing.displacement)
			return displacement<i_RListing.displacement;
		else if(power!=i_RListing.power)
			return power<i_RListing.power;
		else 
			return category<i_RListing.category;
	}


But in my Bikes.txt , All the bikes names are different so it will only sort through alphabetical , it will never enter into the price or the displacement or the power or any other parameter. How can i do that? Sorting by Power , and by displacement , by price.
Hi,

You can edit the function as per your requirements i.e
Sorting by Power , and by displacement , by price.


so, as per your above statement

1
2
3
4
5
6
7
8
9
bool operator <(const listing& i_RListing)
	{
               if(power!=i_RListing.power)
			return power<i_RListing.power;		
               else if(displacement!=i_RListing.displacement)
			return displacement<i_RListing.displacement;
		else if(price!=i_RListing.price)
			return price<i_RListing.price;		
	}
I Have to Sort separately meaning , i have to display the bikes with ascending power and in another place , i have to display with highest displacement , just like that with the price.. But if i use the above code , it will only sort with respect to power only ...

Considering this ,

//Bikes.txt
//Name$price$brand$Displacement$power$category$
Apache180RTR$0.95$TVS$180$17$Naked Sport$
CBR150R$1.4$Honda$150$17$Super Sport$
CBR250R$2.1$Honda$250$25$Sports Tourer$
Duke200$1.5$KTM$200$25$Naked Sport$
Duke390$2.3$KTM$375$43$Naked Sport$
GT250R$3.2$Hyosung$250$27$Super Sports$
Ninja300$4.1$Kawasaki$300$39$Super Sports$
Pulsar200NS$1.05$Bajaj$200$23$Naked Sport$
Pulsar220$0.95$Bajaj$220$21$Sports Tourer$
R15$1.2$Yamaha$150$17$Super Sport$
Street750$5.0$Harley Davidson$750$N.A$Cruiser$


In One display , i wanna sort it by name.
In another , with respect to price meaning

Street750
Ninja300
GT250
Duke390
CBR250
Duke200
R15
Apache
Pulsar 220


I think u got the point , if u check the text file , u will get to know the price , i have just showed the example , but i am not understand how to do it.
Meaning , i have to sort it for like 5 times and display for 5 times w.r.t to each of the parameters like name , price , power etc.
Last edited on
Hi,

Please check the revised code, I think this will satisfy your requirement.

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
class listing
{
public:
	string name;
	string price;
	string brand;
	string displacement;
	string power;
	string category;

	enum ParameterType
	{
		NAME=0,
		PRICE,
		BRAND,
		DISPLACEMNT,
		POWER,
		CATEGORY
	};

	bool Compare(const listing& i_RListing,ParameterType i_PType)
	{
		switch(i_PType)
		{
			case 0:
				return name<i_RListing.name;
			case 1:
				return price<i_RListing.price;
			case 2:
				return brand<i_RListing.brand;
			case 3:
				return displacement<i_RListing.displacement;
			case 4:
				return power<i_RListing.power;
			case 5:
				return category<i_RListing.category;
			default:
				return true;
		};
	}

};


void SortListings (listing A[] , int n, listing::ParameterType i_Type)
{
	 for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
			if(!A[i].Compare(A[j],i_Type))		
				std::swap (A[i],A[j]);			
}


int main()
{
	listing arraListings[2];
	arraListings[0]=listing();
	arraListings[1]=listing();

	arraListings[0].name="Pulser";
	arraListings[1].name="Apache";

	SortListings(arraListings,2,listing::NAME);  

    return 0 ;
}
Thank you Very much Sir. I Got it!
Topic archived. No new replies allowed.