Sort Character With Array (Help Please!)

#include<iostream.h>
#include<iomanip.h>

void main()
{
int NumList[8]={5,34,32,25,75,42,22,2};// Input otomatis dari code
int tmp; //untuk menampung data sementara
cout<<"Data sebelum di urutkan: \n";
for(int i=0;i<=7;i++)
{
cout<<setw(3)<<NumList[i];//stew(3) Mengatur lebar pada tampilan data
}
cout<<"\n\n";
for(int j=7;j>=0;j--)
{
for(int k=j-1;k>-1;k--)
{
if (NumList[j]<NumList[k])//Ascending
{
tmp=NumList[k];
NumList[k]=NumList[j];
NumList[j]=tmp;
}
}
}
cout<<"Data setelah di urutkan: \n";
for(int l=0;l<=7;l++)
{
cout<<NumList[l];
cout<<endl;
}
}


HOW TO CHANGE IT TO BE SORTING A CHARACTER???NOT A NUMBER??? THANKS
You'll only need to change the definition. For example :
1
2
3
4
//int NumList[8]={5,34,32,25,75,42,22,2};
//char CharList[8]={5,34,32,25,75,42,22,2};
char CharList[8]={'5','3','a','v','7','z','m','4'}; //If you want


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream.h>
#include<iomanip.h>

void main()
{
char CharList[8]={5,34,32,25,75,42,22,2};// Input otomatis dari code
char tmp; //untuk menampung data sementara
cout<<"Data sebelum di urutkan: \n";
for(int i=0;i<=7;i++)
{
cout<<setw(3)<<CharList[i];//stew(3) Mengatur lebar pada tampilan data
}
cout<<"\n\n";
for(int j=7;j>=0;j--)
{
for(int k=j-1;k>-1;k--)
{
if (CharList[j]<CharList[k])//Ascending
{
tmp=CharList[k];
CharList[k]=CharList[j];
CharList[j]=tmp;
}
}
}
cout<<"Data setelah di urutkan: \n";
for(int l=0;l<=7;l++)
{
cout<<CharList[l];
cout<<endl;
}
}
Last edited on
THANKS BRO!!! :)
Topic archived. No new replies allowed.