473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

sorting in alphbetical order

i have a array of objects from the class job, i.e.

job teacher[1]
job teacher[2]
job teacher[3]
.............until job teacher[100]

these objects contain the string name of the teachers and

teacher[1].showname(); //return string "Sam Clinton"
teacher[2].showname(); //return string "John Patten"
teacher[3].showname(); //return string "John Dickens"
......... and so on........

i would like to output to screen in ascending order of teacher's name, i.e.

John Dickens
John Patten
Sam Clinton

What should i do? which kind of sorting is better, i don't know any of
them.....
Thanks for your help.
Jul 22 '05 #1
3 1396
"news.hku.hk" <bi******@hkusua.hku.hk> writes:
i have a array of objects from the class job, i.e. job teacher[1]
job teacher[2]
job teacher[3]
............until job teacher[100] these objects contain the string name of the teachers and teacher[1].showname(); //return string "Sam Clinton"
teacher[2].showname(); //return string "John Patten"
teacher[3].showname(); //return string "John Dickens"
........ and so on........ i would like to output to screen in ascending order of teacher's name, i.e. John Dickens
John Patten
Sam Clinton What should i do? which kind of sorting is better, i don't know any of
them.....

Try the most standard sort first. The example below isn't quite what you
want, but it shows how to use sort with user-defined types like your
"job".

#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;

class simple {
public:
int value;
// redefine the '<' operator, so when 2 simple objects are
// compared, the 'value' fields of the objects are compared.
const bool operator<(const simple &rhs) const
{ return this->value < rhs.value; }
};

int main()
{
deque<simple> Q; // An empty, elastic, container.

simple s;
s.value=0;
Q.push_back(s);
s.value=2;
Q.push_back(s);
s.value=1;
Q.push_back(s);

for(int i=0;i<Q.size(); i++){
cout << Q[i].value; cout << " ";
}
cout <<endl;
// Output: 0 2 1

// Now let's sort! sort uses the '<' operator that we created in the 'simple' class
sort(Q.begin(), Q.end());

for(int i=0;i<Q.size(); i++){
cout << Q[i].value; cout << " ";
}
cout <<endl;
// Output: 0 1 2
}

Jul 22 '05 #2
i see what you mean, but how should i modify your code to something that
sort alphabets ??
"Tim Love" <tp*@eng.cam.ac.uk> wrote in message
news:c6**********@pegasus.csx.cam.ac.uk...
"news.hku.hk" <bi******@hkusua.hku.hk> writes:
i have a array of objects from the class job, i.e.
job teacher[1]
job teacher[2]
job teacher[3]
............until job teacher[100]

these objects contain the string name of the teachers and

teacher[1].showname(); //return string "Sam Clinton"
teacher[2].showname(); //return string "John Patten"
teacher[3].showname(); //return string "John Dickens"
........ and so on........

i would like to output to screen in ascending order of teacher's name,

i.e.
John Dickens
John Patten
Sam Clinton
What should i do? which kind of sorting is better, i don't know any of
them.....

Try the most standard sort first. The example below isn't quite what you
want, but it shows how to use sort with user-defined types like your
"job".

#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;

class simple {
public:
int value;
// redefine the '<' operator, so when 2 simple objects are
// compared, the 'value' fields of the objects are compared.
const bool operator<(const simple &rhs) const
{ return this->value < rhs.value; }
};

int main()
{
deque<simple> Q; // An empty, elastic, container.

simple s;
s.value=0;
Q.push_back(s);
s.value=2;
Q.push_back(s);
s.value=1;
Q.push_back(s);

for(int i=0;i<Q.size(); i++){
cout << Q[i].value; cout << " ";
}
cout <<endl;
// Output: 0 2 1

// Now let's sort! sort uses the '<' operator that we created in the

'simple' class sort(Q.begin(), Q.end());

for(int i=0;i<Q.size(); i++){
cout << Q[i].value; cout << " ";
}
cout <<endl;
// Output: 0 1 2
}

Jul 22 '05 #3

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40******@newsgate.hku.hk...
i see what you mean, but how should i modify your code to something that
sort alphabets ??


You can use < to sort alphabetically as well. It's just the same as Tim's
code, all you have to change is the names.

john

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
2
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
4
by: rajtalent | last post by:
hi all, I want to sort the colum when clicks the columnheader using vb.net 2005 .But i receive the following error "Error 1 Overload resolution failed because no accessible 'New' accepts...
3
by: nagmvs | last post by:
Hi to all I have one table with 6 columns and 20 rows.I want to sort each and every column when i click the column name in the table. for sorting i create one more page.when i click the column...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.