I have a Vector that needs to be sorted in Ascending order, as im still a beginner im finding slightly hard to make this work, I've been trying to do it with insertElementAt and compareTo, but I didnt manage. Can anyone Help?
-
Welcome to StackOverflow! There's not enough information in your post. First, you haven't shown your attempted implementation of a sorting algorithm. Second, you haven't said what field in `Product` you would like to sort by (product code, unit price, phrase?). All in all, we'll need a bit more effort from you before we can help. :) – asteri Mar 13 '13 at 17:44
-
Hint: Collections.sort() & Comparator – Mirco Mar 13 '13 at 17:44
-
i have tried Collections.sort, but since i have never used it before i couldnt figure out how to implement it. – KrisF Mar 13 '13 at 17:50
-
Are you really interested in using a `Vector`? It's obsolete in Java. *Vector is considered to be a legacy class* - [see](http://stackoverflow.com/q/1386275/1037210). – Lion Mar 13 '13 at 17:50
-
Yes as my assignment requires me to use vectors and not an array. – KrisF Mar 13 '13 at 17:52
-
out of following fields `String desc, code, phrase; double price;` on the basis of which field do you want to sort Products? – Vishal K Mar 13 '13 at 18:43
-
i want to sort in ascending based on code – KrisF Mar 13 '13 at 18:46
-
@alestanis - How do you figure that's a duplicate? The base language is different (Java vs. C++) – Mike Mar 14 '13 at 15:08
3 Answers
You should decide a criteria based on what you are sorting your vector. You can't just compare two objects, you should pick a "price" or "code", etc. and compare your objects based on that value

- 801
- 2
- 10
- 27
There are two approaches you can take:
Make your
Product
class implement theComparable
interface. This way, you can implement thecompareTo
method which is the base for the sorting operation. This method defines ordering, because it determines which element is higher and which one is lower by returning a positiveint
, zero or a negativeint
.Implement your very own
Comparator
. A comparator defines a comparation method that takes two elements as a base and compares them in the same way thatcompareTo
does. The advantage here is that you can switch ordering criteria by ordering with a differentComparator
.
Finally, the sorting can be performed by the use of any of the overloads of Collections#sort
that takes your collection as an argument and a Comparator
if you decide to use one.

- 9,987
- 4
- 30
- 49
-
I still dont understand how to use the compareTo method, I understand that i need to compare i and i+1, and if i+1 is smaller it replaces i. – KrisF Mar 13 '13 at 18:06
-
@user1965046 That's the point. You implement it but you don't use it yourself. `Collections.sort` will base its sorting algorithm on it or in a `Comparator`, should you decide to implement one. – Fritz Mar 13 '13 at 18:07
-
I have been lookingg at examples all day, i understand how it works I just dont know how to code it. – KrisF Mar 13 '13 at 18:12
-
@user1965046 Implement the interface (`public class Product implements Comparable`) and then override the `compareTo` method. Then, define the ordering criteria (desc, code, phrase or price) and compare the values using `compareTo` for `String`s (it's already implemented) or simply substract the prices between themselves if you use price as the ordering factor (they're doubles, not wrappers, unless you use the wrapper `Double` you have no `compareTo` method there). – Fritz Mar 13 '13 at 18:21
-
I still cant figure out the code, is it possible to help me out with the code or does that go against the rules of the forum? – KrisF Mar 13 '13 at 18:34
-
@user1965046 It's not about rules but about yourself. You can't expect anyone to do your assignment for you, it simply breaks the purpose of trying and learning. Plus, if you were taking a look at examples all day you should have found one that resembles your case (I just googled the topic and found a detailed tutorial involving a list of Products). – Fritz Mar 13 '13 at 18:49
-
can you share the link, because i didnt manage to fnd one similar to mine. – KrisF Mar 13 '13 at 18:51
-
@user1965046 Check [this](http://davidwinter.me/articles/2005/12/08/sorting-a-vector-using-collections-sort/) out, hope it helps somehow if you're this lost. – Fritz Mar 13 '13 at 19:27
To make the Product class Sortable according to code
. You can implement Comparable
interface to your Product class and override compareTo
method. Your Product
class would look like this then:
import java.util.*;
class Product implements Comparable<Product>
{
String desc, code, phrase;
double price;
public Product()
{
this.desc = "";
this.code = ""; // later on irid jinbidel
this. price = 0.0;
this.phrase = "";
}
public Product(String desc, String code, String phrase, double price){
this.desc = desc;
this.code = code; // later on irid jinbidel
this. price = price;
this.phrase = phrase;
}
@Override
public String toString(){
return code;//+", "+desc+", "+price+", "+phrase+".";
}
@Override
public int compareTo(final Product obj) {
return code.compareTo(obj.code);
}
}
To Test if it is sorting Correctly here is simple Demo:
public class TestProduct
{
public static void main(String[] st)
{
Vector<Product> v = new Vector<Product>();
for (int i = 9 ; i > 0 ; i-- )
{
v.add(new Product("MyProduct",i+"Product","Good Product",34.5));
}
System.out.println("Before sorting");//Records inserted in decreasing order of code.
System.out.println(v);
Collections.sort(v);
System.out.println("After sorting");//Records showing in increasing order of code.
System.out.println(v);
}
}

- 12,976
- 2
- 27
- 38
-
but the output doesnt come sorted in ascending order with this code. or is there more i need to add? – KrisF Mar 13 '13 at 19:06
-
@user1965046 : watch my edited answer.. To sort the `Vector` you need to call `Collections.sort(VectorObject)` explicitly..Which you haven't done in your code. – Vishal K Mar 13 '13 at 19:43