Comparar obejtos Integer e String na mesma lista

2 respostas
java
S

Olá pessoal, estou com um problema.
Tenho um List com objetos do tipo Setor, esses objetos tem um atributo String codigo_setor.

O que eu quero é ordenar esses objetos pelo codigo_setor, em ordem crescente, mas que ordene os codigos String que são só números primeiro e depois coloque abaixo desses os códigos que contem letras no codigo_setor.

Aqui um exemplo de tipos de codigo_setor que tenho e como gostaria que ficasse.

Alguém pode me ajudar.

2 Respostas

romero.dias

Amigo, segue um exemplo:

`import java.util.*;

class Setor {

public Setor(String codigoSetor) {

this.codigoSetor = codigoSetor;

}

public Setor() {}

public String codigoSetor;

public String getCodigoSetor() {

return this.codigoSetor;

}

public void setCodigoSetor(String codigoSetor) {

this.codigoSetor = codigoSetor;

}

public String toString() {

return getCodigoSetor();

}

}

class OrderSetor implements Comparator {

public static final String NUMBER_PATTERN = “(\-?\d+\.\d+)|(\-?\.\d+)|(\-?\d+)”;

public int compare(Setor setor, Setor outroSetor) {

String str1 = setor.getCodigoSetor();

String str2 = outroSetor.getCodigoSetor();

if(str1 == null || str2 == null) {

return 0;

}

List split1 = split(str1);

List split2 = split(str2);

int diff = 0;
for(int i = 0; diff == 0 && i < split1.size() && i < split2.size(); i++) {
        String token1 = split1.get(i);
        String token2 = split2.get(i);

        if(token1.matches(NUMBER_PATTERN) && token2.matches(NUMBER_PATTERN)) {
            diff = (int) Math.signum(Double.parseDouble(token1) - Double.parseDouble(token2));
        } else {
            diff = token1.compareToIgnoreCase(token2);
        }
    }
    if(diff != 0) {
        return diff;
    } else {
        return split1.size() - split2.size();
    }

}

private List<String> split(String s) {
    List<String> list = new ArrayList<String>();
    Scanner scanner = new Scanner(s);
    int index = 0;
    String num = null;
    while((num = scanner.findInLine(NUMBER_PATTERN)) != null) {
        int indexOfNumber = s.indexOf(num, index);
        if(indexOfNumber > index) {
            list.add(s.substring(index, indexOfNumber));
        }
        list.add(num);
        index = indexOfNumber + num.length();
    }
    if(index < s.length()) {
        list.add(s.substring(index));
    }
    return list;
}

}

public class Test

{

public static void main(String[] args)

{
List<Setor> setores = new ArrayList<Setor>();
setores.add(new Setor("104R2"));
setores.add(new Setor("92P1"));
setores.add(new Setor("92P3"));
setores.add(new Setor("133"));
setores.add(new Setor("104R1"));
setores.add(new Setor("92P2"));
setores.add(new Setor("MC"));
setores.add(new Setor("MC1"));
setores.add(new Setor("MC10"));

System.out.println(setores);

Collections.sort(setores, new OrderSetor());

System.out.println(setores);

}
}
`


Saída:

Antes
[104R2, 92P1, 92P3, 133, 104R1, 92P2, MC, MC1, MC10]

Depois
[92P1, 92P2, 92P3, 104R1, 104R2, 133, MC, MC1, MC10]

S

E como eu faria para que ficasse assim ??

[133, 92P1, 92P2, 92P3, 104R1, 104R2, MC, MC1, MC10]

Criado 3 de fevereiro de 2016
Ultima resposta 4 de fev. de 2016
Respostas 2
Participantes 2