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

Sorting a structure

hi,
i have a crucial issue: I have to sort a array of structures basing on one of the element, however the problem here is i have to sort it based on strings in the feild. like for example we have a struct called:

struct arr { char a[10];
char b[10];
int a; } a

here a is the key. Say we have arr[10] (ten elements of struct types in array).
now I have a criteria to sort this array bassing on element a which can only stored by a global array. How to go for it. someone please help me.

regards,
Geet
Aug 2 '07 #1
2 1424
r035198x
13,262 8TB
hi,
i have a crucial issue: I have to sort a array of structures basing on one of the element, however the problem here is i have to sort it based on strings in the feild. like for example we have a struct called:

struct arr { char a[10];
char b[10];
int a; } a

here a is the key. Say we have arr[10] (ten elements of struct types in array).
now I have a criteria to sort this array bassing on element a which can only stored by a global array. How to go for it. someone please help me.

regards,
Geet
Just decide on your sorting algorithm and write a sort function.
Forget that they are structs that you are sorting when you're writting it (think of them as ints if you want).
After you're sure your sort works, adapting it to suit structs, or any other type, should be very easy.
Aug 2 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
sort this array bassing on element a which can only stored by a global array
Nothing has to be stored as a global array. Most often global variables are used to avoid having to pass function arguments. This is bad practice.

However, as to your sort, if you ar eusing C you can use qsort. Just write a function that knows how to compare two of your struct variables and returns true if the first variable is less than the second variable. Pass the address of this function to qsort.

If you goe the hard way and write yourown sort, then have an argument to pass in the address of the comparator function, which you will still need to write.
Aug 2 '07 #3

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

Similar topics

5
by: York | last post by:
Lets say I have the following structure struct test_struct { int some_number; char first_name } test_struct s_table
2
by: yee young han | last post by:
I need a fast data structure and algorithm like below condition. (1) this data structure contain only 10,000 data entry. (2) data structure's one entry is like below typedef struct _DataEntry_...
7
by: Karin Jensen | last post by:
Hi I am running a PHP program that connects to an Access 2000 database via ODBC: $results = odbc_exec($connection_id, $sql_select); Is it possible to sort the contents of $results? I wish to...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
11
by: rkbnair | last post by:
I have created a datagrid in my aspx with the 'AllowSorting' property to true. When clicking on the column header, the page refreshes. However the sorting is not done. Am I missing anything? I...
4
by: rushik | last post by:
Hello all, I am using structure in my program, and my aim is to sort this structure based on some optimized sorting algo. structure is struct data { int account;
3
by: Pino | last post by:
HI all, I am learning c++ ( just 1 month) and would want an aid for a code. I have a integer vector of length N. ie 3 3 3 4 4 1
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
12
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath...
3
by: danglez | last post by:
well I have a template of a structure...but when I try to add something to sort the list (an attempt at a bubble sort), I'm running into some problems....any input would be appreciated, thanks ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.