Sorting a vector based on another datas

Z

zelao.itu

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 ?
 
L

Lew

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}

These are vectors, but not Vectors. The conventional Java term for these
structures is "array".
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 ?

You'd be better off declaring a class type that holds the value and rank, then
sort an array or List of that type based on a Comparator that examines the rank.

- Lew
 
Z

zelao.itu

Could I insert these values in a Map Collection and then sort it by
any term that I choose
 
A

Andreas Leitgeb

Could I insert these values in a Map Collection and then sort it by
any term that I choose

Yes, but you can also sort the Vector elements directly
(without shuffling around between various collection types)
by implementing your specific Comparator derived class, which
implements compare(x,y) according to the order you have in mind,
and passing an instance of your Comparator to Collections'
sort(List<T> list, Comparator<? super T> c) method.
(yes, a Vector is also a List)
 
K

Kai Schwebke

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}

Just put the strings as values in a TreeMap using
the ranking as key. The TreeMap then may be accessed sorted by the key
ordering (e.g. the values() method returns an ordered collection).

If you have a large number of elements bubble sort will be very slow --
it is the worst method to sort any kind of data.


Kai
 
D

Daniel Pitts

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}

Just put the strings as values in a TreeMap using
the ranking as key. The TreeMap then may be accessed sorted by the key
ordering (e.g. the values() method returns an ordered collection).

If you have a large number of elements bubble sort will be very slow --
it is the worst method to sort any kind of data.

Kai

Mostly correct. There are worse sorts than bubble sort.
Permutation sort, for example. Try every permutation until you find
one that is sorted.
 
D

Daniel Pitts

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.
 
C

Chris Uppal

Daniel said:
Mostly correct. There are worse sorts than bubble sort.
Permutation sort, for example. Try every permutation until you find
one that is sorted.

Or Random Sort (aka BogoSort)...

My personal favourite, though, is Non-Sort. Here's a Java implementation in
case anyone needs it:

/**
* Non-sort, the fastest known probabilistic sorting algorithm.
*
* This is a probabilistic algorithm, and so is <em>not guaranteed</em>
* to return the correct answer, but &mdash; unlike many sorting
* algorithms &mdash; it <em>is</em> guaranteed that its runtime will
* not be affected by the order of the input elements. Indeed, the runtime
* is O(1) regardless of the input. However &mdash; and again, unlike
* many sorting algorithms &mdash; the order of the input <em>does</em>
* influence the probability of the algorithm producing the correct answer,
* reaching 100% for fully sorted inputs.
*
* @param list the list to be sorted.
* @return the sorted list. Callers should not assume this
* will be != to the input list.
*/
public static <X> java.util.List<X>
nonSort(java.util.List<X> list)
{
return list;
}


I also have an in-place version (plug-compatible with
java.util.Collections.sort(List)), if required.

-- chris
 
I

Ian Wilson

Chris said:
Daniel Pitts wrote:




Or Random Sort (aka BogoSort)...

My personal favourite, though, is Non-Sort. Here's a Java implementation in
case anyone needs it:

/**
* Non-sort, the fastest known probabilistic sorting algorithm.
*
* This is a probabilistic algorithm, and so is <em>not guaranteed</em>
* to return the correct answer, but &mdash; unlike many sorting
* algorithms &mdash; it <em>is</em> guaranteed that its runtime will
* not be affected by the order of the input elements. Indeed, the runtime
* is O(1) regardless of the input. However &mdash; and again, unlike
* many sorting algorithms &mdash; the order of the input <em>does</em>
* influence the probability of the algorithm producing the correct answer,
* reaching 100% for fully sorted inputs.
*
* @param list the list to be sorted.
* @return the sorted list. Callers should not assume this
* will be != to the input list.
*/
public static <X> java.util.List<X>
nonSort(java.util.List<X> list)
{
return list;
}


I also have an in-place version (plug-compatible with
java.util.Collections.sort(List)), if required.

You forgot to specify the licence. GPL? BSD? Commercial?
If someone uses it in a commercial product do you want attribution?
"Sort function provided by Chris Uppal"
;-)
 
Z

zelao.itu

Thanks a lot for everybody!!!!
it's really solved my problem!!!

thanks!!

**Non-Sort!!! LOL!!! hehe =D
 
C

Chris Uppal

Ian Wilson wrote:

[me:]
public static <X> java.util.List<X>
nonSort(java.util.List<X> list)
{
return list;
}
[...]

You forgot to specify the licence. GPL? BSD? Commercial?
If someone uses it in a commercial product do you want attribution?
"Sort function provided by Chris Uppal"
;-)

I did consider that, but it's difficult to know how to phrase the licence...

================
Copyright (c) Chris Uppal 2007.
Permission is hereby granted to use and distrubute the software for all
purposes subject to the following conditions:

NO WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO (etc, etc, etc -- you know the rest).

The above copyright notive, this license text, and the name "Chris Uppal",
must NOT appear in any copies of this software. If the program in which
it is embedded is such that a user may review licensing and copyright
attribution; then credit must NOT be given to "Chris Uppal" and that
name must NOT appear in any form or as any part of that information.
================

But it seems a little clumsy -- and in any case would be incompatible with the
licensing terms of other software I've made available. (Would also be
incompatible with the GPL, but who cares...)

-- chris
 
R

Randolf Richardson

Ian Wilson wrote: [sNip]
You forgot to specify the licence. GPL? BSD? Commercial?
If someone uses it in a commercial product do you want attribution?
"Sort function provided by Chris Uppal"
;-)

I did consider that, but it's difficult to know how to phrase the
licence...

================
Copyright (c) Chris Uppal 2007. [sNip]
NO WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO (etc, etc, etc -- you know the rest).

The above copyright notive, this license text, and the name "Chris
[sNip]

Whoops: notive
But it seems a little clumsy -- and in any case would be incompatible
with the licensing terms of other software I've made available.

You need get in touch with a lawyer about the wording. If you don't do
this, then you might not have things covered properly in the spirit you
intended.
(Would also be incompatible with the GPL, but who cares...)

How so? Because you don't want credit for it? By the way, I don't care
either. =D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top