Jump to content

remeber index after sorting an array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
yellowzelo

yellowzelo

    Newbie

  • Members
  • PipPip
  • 18 posts
Hi,

I think that an example illustrates my problem the best. This is my code:

String[] array = new String{"Peter", "Eva", "Martin", "Jane", "Adam"}; //array of strings

java.util.Arrays.sort(array);

//array is now {"Adam", "Eva", "Jane", "Martin", "Peter"}


I need to find out old indexes of some names in that array (ie. indexes before sorting). The function I am looking for would take as a parameter new index and return the old index.


int index = somefunction(2); // 2 is new index of "Jane"

System.out.print(index); //prints 3 - old index of "Jane"

Do you have any idea how to do that?
This was just example, but in real my array size is cca 10^6.

Thank you.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Why not create an array of pairs? The first value is the string, the second is the original index. Have the sort algorithm function on the strings. Then you have the original index in the sorted array.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
yellowzelo

yellowzelo

    Newbie

  • Members
  • PipPip
  • 18 posts
That could work, but how to declare an array of pairs? And would java.util.Arrays.sort() work for sorting that array?

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Yes, Arrays.sort would work.

class Pair implements Comparable<Pair> {
        String s;
        int nIdx;

       public int compareTo(Pair p) {
               return 0; // comparator code here...
       }
}

Pair[] p = new Pair[10];

for (int i=0;i<10;i++) {
       p[i] = new Pair("String",i);
}

Arrays.sort(p);

Edited by chili5, 17 December 2009 - 01:20 PM.


#5
yellowzelo

yellowzelo

    Newbie

  • Members
  • PipPip
  • 18 posts
Thank you for the answers, but it still doesn't work.

I used the code you posted, but it prints this error:

Quote

myapp.Pair is not abstract and does not override abstract method compareTo(myapp.Pair) in java.lang.Comparable


What is compareTo() method for a what I do I have put in it?

Thanks.

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I think that Chili didn't quite do the comparable interface right, he may be more used to comparators.

class Pair implements Comparable<Pair>
{
    String s;
    int nIdx;

    @Override
    public int compareTo(Pair other)
    {
        int result = 0;
        // Compare other somehow
        return result;
    }
}

Wow I changed my sig!

#7
yellowzelo

yellowzelo

    Newbie

  • Members
  • PipPip
  • 18 posts
Thank you, ZekeDragon.

But how can I assign values to variables s, nIdx? I've tried this code but it prints error:

Pair[] p = new Pair[10];


for (int i=0;i<10;i++) 

       p[i] = new Pair("String",i);


Quote

Cannot find symbol
symbol: constructor Pair(java.lang.string.int)
location: class myapp.Pair


And what is compareTo() method used for? Should I write some code in it?

PS: sorry for stupid questions, I am the beginner in Java. :blushing:

Thanks.

#8
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Ahh, I see. Well do you understand the basics of Object-Oriented Programming? What Chili showed you there was a constructor he had assumed you'd implement (you will need to implement everything in the object). Those of us who are helping you cannot really change your function because of two reasons, one we don't know exactly what you're trying to do, but also because the point of doing all this is so you actually learn to write the code on your own, which means doing your own research and experimentation and learning how to deal with the compiler complaining at you! That's just part of programming.

A constructor is a function that does not return a value and is named the same name as the object it is in. For example:
class Pair implements Comparable<Pair>
{
    public Pair(String inStr, int inInt)
    {
        // It's up to you to implement it!
    }
    //...
}
All you should have to do is set the private values in the Pair Object to the values passed to the constructor and you should be golden. What the compiler meant in your last error was that the compareTo() function it expected to be there wasn't, and the interface you are implementing requires that function to be implemented. An Interface is much like a class, except it only provides public abstract functions and public static final variables. These are useful as wrappers to implementing classes so they can be treated the same in functions. This is one of the strengths of OOP, and you'll get much use out of it.

Anyway, I hope that helped. Remember that constructors are an important part of an object, and we didn't implement the complete object for you. I highly recommend reading through the Java Trails from the very beginning, that should help you understand Java more.
Wow I changed my sig!

#9
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Great explanation! +rep

I made a small mistake with the comparable interface... but I fixed that. :P

Quote


And what is compareTo() method used for? Should I write some code in it?

compareTo is a method for comparing two objects. This is the essence of sorting. This tutorial talks about comparators.

#10
yellowzelo

yellowzelo

    Newbie

  • Members
  • PipPip
  • 18 posts
Thank the both of you.

I created array this objects and the sorting works:

    static class Pair implements Comparable<Pair> {

        String string;

        int index;


        public Pair(String s, int i){

            this.string=s;

            this.index=i;

        }


        //sort Pairs based on lexicographic order of strings

        @Override

        public int compareTo(Pair other){

            return this.string.compareTo(other.string);

        }

    }

BUT the problem is that sorting array of this Pairs is cca 1.8x slower on my machine than sorting just array of strings. Do you have a clue how to make running it faster?