Re: Sorting a vector based on another datas
- From: "Daniel Pitts" <googlegroupie@xxxxxxxxxxxxx>
- Date: 31 Jan 2007 10:37:31 -0800
On Jan 31, 3:13 am, "zelao....@xxxxxxxxx" <zelao....@xxxxxxxxx> wrote:
Hey everybody, I'm having troubles with sorting a vector based in
another vector:
Example:
String [] models = {"Anna","Gisele","Yasmin"}
int [] ranking ={3,1,2}
....After sorting it might be this :
models = {"Gisele","Yasmin","Anna"}
ranking = {1,2,3}
It's because i sorted the models' vector based on ranking vector, and
it could accept duplicate datas;
So how can i do that ??????
I think about do a bubble sort in ranking vector and when the
condition met (like ranking[0] > ranking[1]) I swap both vectors, but
I don't think it's the best way. There is another one ?
The best way would be to avoid paralell arrays.
class Model implements Comparable<Model> {
private final int ranking;
private final String name;
public Model(int ranking, String name) {
this.ranking = ranking;
this.name = name;
}
public int compareTo(Model o) {
return ranking < o.ranking ? -1 : ranking == o.ranking ? 0 : 1;
}
public String toString() {
return "Rank " + ranking + ": '" + name + '\'';
}
}
public class SortModels {
public static void main(String...args) {
Model[] models = new Model[] {
new Model(3, "Anna"),
new Model(1, "Gisele"),
new Model(2, "Yasmin")
};
Arrays.sort(models);
for (Model model: models) {
System.out.println(model);
}
}
}
This program will print out:
Rank 1: 'Gisele'
Rank 2: 'Yasmin'
Rank 3: 'Anna'
Hope this helps,
Daniel.
.
- References:
- Sorting a vector based on another datas
- From: zelao.itu@xxxxxxxxx
- Sorting a vector based on another datas
- Prev by Date: Re: Sorting a vector based on another datas
- Next by Date: Re: Web start on os X : Cannot open associated file.
- Previous by thread: Re: Sorting a vector based on another datas
- Next by thread: How can i optimize the web services client access code
- Index(es):
Relevant Pages
|