The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sorting : satyam_sat Shell Programming and Scripting 3 04-15-2008 01:30 PM
regarding file sorting phani_sree High Level Programming 1 02-02-2007 03:04 PM
sorting in awk mahabunta Shell Programming and Scripting 3 01-31-2007 01:56 AM
awk sorting Jaken Shell Programming and Scripting 4 05-21-2006 10:54 AM
Sorting mp3 penguin-friend Shell Programming and Scripting 3 05-13-2005 04:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-05-2008
ronix007 ronix007 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 32
Sorting in C++..

Hi,

I need to do a sorting of 2 arrays. One array contains the values of both integer and character and other array can be anything. For example:

Array[1]={'1L','2C','NULL','23L','11L','4C','10L','9C'}
Array[2]= {'01-02-13-1x','02-11-23-3s','00-12-13-5f','NULL','22k',}

If any of these arrays having 'NULL' values,then we should not sort them. I need to sort these arrays so that array[1]=array[2], will be same as it was before sorting.

Please let me know if you need anymore information. Thanks in advance.

Rohit..
  #2 (permalink)  
Old 03-06-2008
WebKruncher WebKruncher is offline VIP Member  
VIP Member
  
 

Join Date: Dec 2007
Location: Simi Valley, CA
Posts: 29
Maps

I think I can help you out, but I might need a better understanding of what you're trying to do. The map class might be what you're looking for. If your not used to templates, the syntax is a bit intimidating, but it becomes clear if you work with the simplest case first. I normally inherit a map base into a custom object like this:

struct CustomMap : map<string,string>
{
} stuff;

stuff["b"]="b";
stuff["a"]="a";
stuff["0"]="0";

See map - C++ Reference

The map sorts by the key ("a1","a2", etc.) and the sort can be customized.

It's easy to iterate through the sorted data set using an iterator. Most of the time I add in an extraction operation so I can dump the sorted list straight to the target.

Hope that helps.
-Jmt
  #3 (permalink)  
Old 03-06-2008
ronix007 ronix007 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 32
Ok. Take it in a simple way. Like, I need to sort the two arrays, where I have given the examples of the values. e.g. Array1[0] will map with Array2[0] values and so on, both before and after the sorting. Incase, we have any NULL value inbetween for any array, then we need to drop that element.

I need a solution with using simple bubble sorting (Or similar kind of sorting) as we do not use containers or STL for this. Please let me know if you need anymore information or still having any doubts regarding the same.

Thanks,
Rohit..
  #4 (permalink)  
Old 03-06-2008
WebKruncher WebKruncher is offline VIP Member  
VIP Member
  
 

Join Date: Dec 2007
Location: Simi Valley, CA
Posts: 29
algorithms, libraries, re-inventions

I'm having a little trouble understanding your pseudo code, but it sounds like it might be best to just approach the problem as if it is one array, and one sort. Then, if the second array needs to sort differently, just change the comparison method in the sort.

The bubble sort algorithm is in a lot of different books, but it makes sense to just use a library routine. To sort a single array, the set<> class might work out for you. Also, the standard library does provide a sort family.

-Jmt
  #5 (permalink)  
Old 03-06-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
The set suggestion sounds good excepting the rigorous definition of a set which is an unsorted collection of objects.
It sounds like what you want to do is some kind of set difference or intersection type ops on an array and use NULL as an empty marker. is this correct?
  #6 (permalink)  
Old 03-06-2008
WebKruncher WebKruncher is offline VIP Member  
VIP Member
  
 

Join Date: Dec 2007
Location: Simi Valley, CA
Posts: 29
hmmm, I better go look closer at the ISO, but I think set<> is always sorted.
See set - C++ Reference
GCC seems to think the set is sorted.

If the NULL is listed in the data as the character string "NULL", and that needs to be interpreted as 0, then maybe create a string derivative and specialize it's comparison method, then use a set of your specialized strings.

Last edited by WebKruncher; 03-06-2008 at 05:09 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0