sortowanie iteracyjne tablicy stringów

0

mam problem z sortowaniem tablicy
a mianowicie

  for(int i=0; i<=builder.length(); i++)
	{
	int poz=i;
	String element=k[i];
	for (int k3=i+1; k3<builder.length(); k3++)
		{
		if(element>k[k3])

			{
			element=k[k3];
			poz=k3;
			}
		}
		k[poz]=k[i];
		k[i]=element;
          i++;
          }

problem jest z warunkiem if jak obejść aby to mogło działać
ogólnie sortowanie było to pod typ int więc przerobiłem na string.
prosze o pomoc

0

Użyj metody compareTo.

         static public void main(String args[]) {
		String[] strings = {"a", "c", "f", "d", "z", "m", "b", "a" };
		for(String s : strings) System.out.print(s + ",");
		for(int i = 0; i< strings.length; i++) {
			int p = i;
			String element = strings[i];
			for (int j = i + 1; j < strings.length; j++) {
                if(element.compareTo(strings[j]) > 0) {
                	element=strings[j];
                    p = j;
                }
			}
			strings[p] = strings[i];
			strings[i] = element;
		}
		System.out.println();
		for(String s : strings) System.out.print(s + ",");
	}

Zdecydowanie prościej jednak będzie tak :-)

       static public void main(String args[]) {
		String[] strings = {"a", "c", "f", "d", "z", "m", "b", "a" };
		for(String s : strings) System.out.print(s + ",");
		Arrays.sort(strings);
		System.out.println();
		for(String s : strings) System.out.print(s + ",");
	}

1 użytkowników online, w tym zalogowanych: 0, gości: 1