user1876712
user1876712

Reputation: 113

How to sort the vector of integers by its index?

I want to sort a vector of integers using java comparable or comparator interface. Once the sorting is done i should be able to find its indices as of the before.

I also want to know the differences between the comparable and comparator interface on when to use what?

Upvotes: 0

Views: 83

Answers (2)

Stephen C
Stephen C

Reputation: 718768

It sounds like you should be using either 2 vectors/lists or 2 maps.

There is no way that you can represent both the current position and the old position with just a List<Integer>.

Upvotes: 0

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

A comparator is an interface that lets you compare two objects. Comparable lets you compare the current object to another. In your case, you'd want a comparator since you can't change the Integer class to make it Comparable. You could subclass it, but that would be confusing.

I'm not sure what you mean by indices as of before. It sounds like you want some mapping between the old and new values. Why not just loop through the original list for the # to find the old index?

Upvotes: 1

Related Questions