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

Sorting a linked list of structs on multiple parameters.

3
Hi
I have a linked list of struct.
with fields last name, first name, age

I want to sort this linked list based on the parameter provided by the user.

If the user provides last name, i want that list be sorted by last name. If 2 or more people have same last name then it be sorted by their first name, and if first names are also same for any then by age.

Same for first name sorting. initial sort by first name, then last name and then age


I haven't been able to figure out how to do this.Since I have a large file of this data, i would have to find an efficient solution.


Thanks in Advance

Jagu
Jun 21 '10 #1
7 4669
donbock
2,426 Expert 2GB
You only need to look at the next key if the previous one was identical for the two records.

"If the user provides last name, i want that list be sorted by last name."
So what do you want to do if one record has a last name but the other doesn't?

Do you want to do a case-insensitive comparison?
How do you want to handle punctuation (such as dash or apostrophe)?
That is, do you want O'Reilly to match oreilly?
Jun 21 '10 #2
jagu22
3
Each struct has a last name, first name and age.

Punctuation be different. O'Reilly and oreilly be different.
Comparisons be case-sensitive.

I m more concerned in making it efficient , I can do it my way. But I need to have an efficient data structure use.
I have all the records in a linked list. And it will contain around 1000's of nodes.

So how to sort them is a major concern to me. Do i sort the list everytime I have to call the function. Or do I create 3 lists and keep them sorted throughout the program execution.
Jun 21 '10 #3
donbock
2,426 Expert 2GB
Age changes over time; and it changes on different days for different people. Do you mind if the sort order changes from one day to the next?

If so, then you should sort by birthdate (year-month-day) rather than by age.

In which order do you wish to sort by age: youngest first or oldest first?

Do you compute the age from birthdate and current date on the fly or is age explicitly stored in the database? If it is explicitly stored in the database, then are you worried that the value might be out-of-date?
Jun 21 '10 #4
donbock
2,426 Expert 2GB
Now I understand. All records have first name, last name, and age. The user may ask for a list sorted by last name or the user may ask for a list sorted by first name.

You have two fundamental key strategies: either construct the sort-key on the fly each time you need to resort or construct the key once when each record is created and store it in the record. Either way, the key is constructed by concatenating the three fields together in the appropriate order. You want to separate the fields with a character that is "further to the right" than any character that could legitimately appear in the fields. If you choose the latter strategy then you will actually have to store both sort-keys. Storing the sort-key(s) in the records makes sense the more times you have to resort the list.

You have two fundamental sort strategies: either resort each time the user asks for a list or sort the list both ways at the very beginning and respond to user requests by merely traversing the list rather than resorting it. If you choose the latter strategy then you will need two sets of links, one for each sort-key.

Can the user add, delete, or change entries? If so (and if you choose the latter sort strategy), then you need to adjust the list order every time one of these activities occurs. Adjustments of this kind ought to be possible without resorting the entire list.
Jun 21 '10 #5
jagu22
3
Can you tell me more how to sort on the key.
my last name, first name can be 10 characters each. and age 3 digits.
I understand that i concatenate the 3.
lastname+firstname+age

and that would go to 23 characters. But how do i sort them. for the missing characters i put Space . like if last name is of 5 chars. I put 5 spaces before concatenating the first name.

But how do i sort them.
Jun 21 '10 #6
donbock
2,426 Expert 2GB
You need to pick a field-separator character. It should be a lower sort-value than any character that might legitimately appear in any your fields, but it can't be '\0' because you need to reserve that code for terminating the string. I suggest '\001' -- it is the smallest non-null character value. This wouldn't work if your compiler's character encoding had a printable character for that value.

Choosing such a small value for the field separator eliminates the need to pad the first or last names with spaces.

You need to pad the age field with zeroes to insure it sorts properly.

For example, for a record equal to
Thomas Hardy, age 39
then the last-first-age key is "Hardy" "\001" "Thomas" "\001" "039";
and the first-last-age key is "Thomas" "\001" "Hardy" "\001" "039";

You can compare keys with the strcmp function.
Jun 21 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
But how do i sort them.
Are you able to sort an array of int?

If so, then there is a place in your code where you compare two ints.

When you sort your structs, there will be a place in your code where you compare two structs. Just call your function that compares two of your sort keys.

It would be helpful to have functions that a) create sort keys, b) compare to struct objects (this function should have the same return values as strcmp since this is a we--understood behavior), c) swaps two struct variables.

If you have code that sorts an array of int you should be able to modify it by removing the int stuff and substituting your functions above.
Jun 22 '10 #8

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

Similar topics

10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
3
by: sugaray | last post by:
hi, i have to build a linked-list which has another sturcture _score as it's data entry, so how can i sort such linked-list based on, let say, history score into proper order...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
2
by: VirusFree | last post by:
hi guys, i wrote an article about an algorithm i came up with, (which actually is based on hash algorithms..) and it's for sorting linked lists... (up to 10 times faster than mergesort on my...
6
by: mbk006 | last post by:
I am new for linked lists. So please help. So any one kindly send the algorithms for creating single linked list, sorting linked list and Deleting link list
5
by: Draggonn | last post by:
Hi all. I wanted to ask if this code for sorting linked list was ok. what i mean is, is it written without too much mess :P such as unnecessary lines and pointers, etc.. what i did was make a second...
8
by: Jeff Bown | last post by:
Consider implementing a (doubly) linked list. The simplest strategy is to provide functions item_t add_item(item_t predecessor); void delete_item(item_t item); where add_item allocates memory...
2
by: ihimitsu | last post by:
Hi friends guide me to sorting java list contains multiple list objects
5
by: kylie991 | last post by:
I'm confused as to why this isnt working.. am i doing this right?? I have to create a function that does the following: - Insert a word to the linked list so that the list // ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.