473,499 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Sort the following

8 New Member
Hi,
Can anyone give me a solution for the following..

abc0/1/0/1
abc0/1/0/3
abc0/1/0/2
abc0/1/0/4
..
.
.
.abc0/1/0/9
abc0/1/0/10

don't think that its already sorted..assume these are values to be sorted.
i gone for sorting where objects are compared, while doing so it is sorted as follows

abc0/1/0/1
abc0/1/0/10
abc0/1/0/2
.
.
.
etc
abc0/1/10 is coming after abc0/1/0/1. but actually it should be after abc0/1/0/9.

can anyone give an optimized solution.

Thanks..
Babu shanmugham
Oct 27 '06 #1
3 1420
r035198x
13,262 MVP
Hi,
Can anyone give me a solution for the following..

abc0/1/0/1
abc0/1/0/3
abc0/1/0/2
abc0/1/0/4
..
.
.
.abc0/1/0/9
abc0/1/0/10

don't think that its already sorted..assume these are values to be sorted.
i gone for sorting where objects are compared, while doing so it is sorted as follows

abc0/1/0/1
abc0/1/0/10
abc0/1/0/2
.
.
.
etc
abc0/1/10 is coming after abc0/1/0/1. but actually it should be after abc0/1/0/9.

can anyone give an optimized solution.

Thanks..
Babu shanmugham
Are you saying you already have a sort function that is not sorting correctly? If so post it so that we can work from there.
Oct 27 '06 #2
bashanmu
8 New Member
Are you saying you already have a sort function that is not sorting correctly? If so post it so that we can work from there.
Hi here is the code...give me the alternative...have a look at compare method, the r comparing the strings.
sort( int col, boolean asc )
{
curntcol = col;
curntcolsort = asc;
heapsort( col, asc );
}
heapSort( int col, boolean asc, int count ) {
build( col, asc, count );
for ( int node = count - 1; node > 0; node-- )
{
swap( 0, node );
traver( col, asc, 0, node );
}
}
build( int col, boolean asc, int count )
{
for ( int node = count/2 - 1; node >= 0; node-- )
{
traver( col, asc, node, count );
}
}
traver( int col, boolean asc, int node, int count )
{
Comparable firstval = getval( node, col );
**** Index of first descendant of node
int secindex = 2 * node + 1;
while ( secindex < count )
{
Comparable secval= getval( secindex, col );
**** Is there a second descendant?
if ( secindex + 1 < count )
{
Comparable nextSecval= getval( secindex + 1, col );
if ( ( asc && compare( nextsecval, secval) > 0 ) ||
( !asc && compare( nextsecval, secval) <= 0 ) )
{
secindex++;
secval= nextsecval;
}
}
****node already satisfies the heap property
if ( ( asc && compare( firstval, secval) >= 0 ) ||
( !asc && compare( firstval, secval) < 0 ) )
{
break;
}
*****Exchange the labels of node and descendant
swap( node, secindex );
node = secindex;
secindex = 2 * node + 1;
}
}
int compare( Object obj1, Object obj2) {
if ( ( obj1 == null ) && ( obj2 == null ) )
{
return 0;
}
else if ( obj1 == null )
{
return -1;
}
else if ( obj2 == null )
{
return 1;
}
return ((Comparable)obj1).compare((Comparable)obj2);
}
swap( int a, int b ) {
ArrayList list = getCurrentList();

swap( list, a, b );
}
swap( ArrayList aList, int a, int b ) {
int size = aList.size();

if ( ( aList == null ) || ( a < 0 ) || ( a >= size ) || ( b < 0 ) || ( b < size ) )
{
Object tempObject = aList.get( a );

aList.set( a, aList.get( b ) );
aList.set( b, tempObject );
}
}
Oct 27 '06 #3
r035198x
13,262 MVP
Hi here is the code...give me the alternative...have a look at compare method, the r comparing the strings.
sort( int col, boolean asc )
{
curntcol = col;
curntcolsort = asc;
heapsort( col, asc );
}
heapSort( int col, boolean asc, int count ) {
build( col, asc, count );
for ( int node = count - 1; node > 0; node-- )
{
swap( 0, node );
traver( col, asc, 0, node );
}
}
build( int col, boolean asc, int count )
{
for ( int node = count/2 - 1; node >= 0; node-- )
{
traver( col, asc, node, count );
}
}
traver( int col, boolean asc, int node, int count )
{
Comparable firstval = getval( node, col );
**** Index of first descendant of node
int secindex = 2 * node + 1;
while ( secindex < count )
{
Comparable secval= getval( secindex, col );
**** Is there a second descendant?
if ( secindex + 1 < count )
{
Comparable nextSecval= getval( secindex + 1, col );
if ( ( asc && compare( nextsecval, secval) > 0 ) ||
( !asc && compare( nextsecval, secval) <= 0 ) )
{
secindex++;
secval= nextsecval;
}
}
****node already satisfies the heap property
if ( ( asc && compare( firstval, secval) >= 0 ) ||
( !asc && compare( firstval, secval) < 0 ) )
{
break;
}
*****Exchange the labels of node and descendant
swap( node, secindex );
node = secindex;
secindex = 2 * node + 1;
}
}
int compare( Object obj1, Object obj2) {
if ( ( obj1 == null ) && ( obj2 == null ) )
{
return 0;
}
else if ( obj1 == null )
{
return -1;
}
else if ( obj2 == null )
{
return 1;
}
return ((Comparable)obj1).compare((Comparable)obj2);
}
swap( int a, int b ) {
ArrayList list = getCurrentList();

swap( list, a, b );
}
swap( ArrayList aList, int a, int b ) {
int size = aList.size();

if ( ( aList == null ) || ( a < 0 ) || ( a >= size ) || ( b < 0 ) || ( b < size ) )
{
Object tempObject = aList.get( a );

aList.set( a, aList.get( b ) );
aList.set( b, tempObject );
}
}
Your compare is treating the values as strings. You need to write a specialised compare function for this which treats the numerical part as numbers not as strings.

split the strings into 4 parts each. String [] values = string.split("/"), will do.
then compare will do something like
if(values[0].compareTo(values2[0]) > 0)
return true;
else if(Integer.parseInt(values[1]) > Integer.parseInt(values1[1]))

etc
Oct 27 '06 #4

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

Similar topics

4
3270
by: Seeker | last post by:
Hi, I have an array of objects. My object definition is given below: function tempArray(code,height,weight) { this.code = code; this.height = height; this.weight = weight; }
10
1709
by: Paul Schneider | last post by:
I want to sort a class derived from std::vector with STL sort.: template<typename T, typename fitParaType, typename fitResType> class Manipulator{ // shouldn't I now be able to access private...
4
2781
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
7
2428
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
21
3167
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
16
16358
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
4
1533
by: G .Net | last post by:
Hi I have a question which I hope you can help with. I am setting the DataSource of a DataGrid to be a DataView. I am sorting the DataView by various fields which include a Date. When I...
48
4406
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
4129
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
2
2535
by: gaurav kashyap | last post by:
Hi all, I am using python version 2.3.in a program , I have called the sort function.Wherein, a.sort(reverse=True) is giving the following error: TypeError: sort() takes no keyword arguments....
0
7229
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...
1
6905
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
7395
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
5485
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,...
1
4921
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...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.