473,397 Members | 1,950 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,397 software developers and data experts.

Sorting names and IDs

19
Hey, guys. I've been using a selection sort for sorting 1d arrays, but now i've got a 2d array with names and ids.

Expand|Select|Wrap|Line Numbers
  1. string employee[name][id]
and i can't figure out how to change the algorithm to sort just the names and have the ids still attached to them such as:

Expand|Select|Wrap|Line Numbers
  1. Smith, John 12
  2. Q, Suzie 47
  3. Ford, Henry 22
yields

Expand|Select|Wrap|Line Numbers
  1. Ford, Henry 22
  2. Q, Suzie 47
  3. Smith, John 12
any help would be much appreciated.
Nov 12 '07 #1
4 1262
Ganon11
3,652 Expert 2GB
What are you sorting by; names, or IDs?

Basically, where you swap values, you'll have to swap the name values as well as the id values, which will involve re-writing your swap function.
Nov 12 '07 #2
toefraz
19
I'm sorting by names. I think I'll just use strcat to put the name and id into one string and just sort using a 1d algorithm then split them apart.

Or do you think another way would be easier/more efficient?
Nov 12 '07 #3
Ganon11
3,652 Expert 2GB
That will certainly work, but the suggestion I gave will also work, and will avoid any messy string work. What sorting function are you using? Whatever it is, when it swaps two values, you'll just rewrite that part to swap both the name and ID values. It's not hard - probably involving maybe 2 or 3 more lines of code at the most.
Nov 12 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
I'm sorting by names. I think I'll just use strcat to put the name and id into one string and just sort using a 1d algorithm then split them apart.
You sort variables. Your variables contain names plus an ID. That means your variables are struct variables. That means you have a 1D array of struct variables. That means you swap struct variables based on a comprare of the names inside the variables.

"Putting things together and later taking them apart" is a poor design approach.

Try to keep every thing in terms of a single variable. Then you can write functions to swap variables, to assign one to another, to copy, to assign, etc.
Nov 12 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: bissatch | last post by:
Hi, I am about to write an application that will display all pdf files in a folder and display them by file name in order of date. Is there an easy way of doing this? I was thinking about...
4
by: Charles A. Lackman | last post by:
Hello I have created a Dataview and have sorted it on the Date and ShiftNumber columns this works great, but when I want to use the Find Method it gives me the following error: Expecting 2...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
0
by: Robert Brinson | last post by:
Hello all! I'm running .NET Framework 1.1 using VS.NET 2003. I've got a mystery with a DataGrid. Below is the definition of the DataGrid from my aspx page: </asp:datagrid><asp:datagrid...
10
by: nk | last post by:
Hi, I'm a newbie. The code below sorts names entered. ---------------------------------------------------------------------------- #include <stdio.h> #include <conio.h> #include <string.h>...
2
by: zfareed | last post by:
I managed to sort a 2-dim array of lastnames but I am having trouble with printing the corresponding first names which are stored in a different 2-dim array. Here is my code for: ...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
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...
1
by: dorandoran | last post by:
The sort on the childgrid is not working; nothing happens when I click on the each column header for sort. (I followed Satay's sample: http://www.codeproject.com/KB/aspnet/EditNestedGridView.aspx)...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.