How do i Write a method to sort the parallel arrays according to the ascending order of names, if the two other arrays are numeric and not string like the name array? -
-
public void sortD(String[]name,int[] age, double[] mark)
-
{
-
for (int i = 0; i < age.length - 1; i++)
-
{
-
for (int j = 0; j < age.length -1-i; j++)
-
{
-
if (age[j] > age[j + 1])
-
{
-
/*
-
*Swap age with lowest age
-
*/
-
int ageTemp = age[j];
-
age[j] = age[j + 1];
-
age[j + 1] = ageTemp;
-
-
/*
-
Swap name using the same index of the age
-
*/
-
-
String nameTemp = name[j];
-
name[j] = name[j + 1];
-
name[j + 1] = nameTemp;
-
-
double markTemp=mark[j];
-
mark[j]= mark[j + 1];
-
mark[j + 1]=markTemp;
-
}
-
-
}
-
You have this line
This determines the order of the data in your bubble-sort like algorithm.
Just exchange it with - if (name[j].compareTo(name[j + 1]) > 0)
and it sorts it by name ascending instead.
If you want a high code quality, use object orientation, that means don't use parallel arrays to store the data, instead create a class Person and store your data in a single array of this class (or even better, in a List instead of in array), using Getter/Setters or fields of this class. -
// keep all data together in a single object
-
class Person {
-
public String name;
-
public int age;
-
-
// this method is for nice printing
-
public String toString() {
-
return "name: " + name + " age: " + age;
-
}
-
}
-
-
// declare storage for many object instances
-
List personList = new ArrayList();
-
-
// setup demo data
-
String[] name = {"B-name", "A-name"};
-
int[] age = {42, 18};
-
-
// store data
-
for (int i = 0, length = name.length; i < length; i++) {
-
Person p = new Person();
-
p.name = name[i];
-
p.age = age[i];
-
personList.add(p);
-
}
-
And second, don't reinvent sorting algrithms but use Collections.sort() -
// sort by name
-
class NameComparator implements Comparator {
-
public int compare(Object p1, Object p2) {
-
return ((Person)p1).name.compareTo(((Person)p2).name);
-
}
-
}
-
Collections.sort(personList, new NameComparator());
-
-
// print results
-
System.out.println("sorted person array=" + personList);
-
So if you run these two code snippets, it will print out: -
sorted person array=[name: A-name age: 18, name: B-name age: 42]
-
If you need more fields like "mark", "addresss" etc, put them into your Person class. If you need to sort by them, write a Comparator for each of them, for example MarkComparator, AddressComparator etc., and sort them by using -
Collections.sort(personList, new MarkComparator());
-
Collections.sort(personList, new AddressComparator());
-
You can even use anonymous inner classes for that, for example you do not declare a AgeComparator, but sort directly this way: -
Collections.sort(personList, new Comparator() {
-
public int compare(Object p1, Object p2) {
-
int age1 = ((Person)p1).age;
-
int age2 = ((Person)p2).age;
-
return (age1 > age2 ? 1 : (age1 == age2 ? 0 : -1));
-
}
-
});
-
You can even improve more, for example by defining a constructor for your class Person that takes as arguments all the data beloging to one person (Age, Name, etc.). Or if you don't read in arrays with the name, age etc. in the first way and then later assign them to a person list in a separate for-loop, but assign the data directly to a person and then put him in the list. And so on.
2 7151
I don't know what you question is. Is the code throwing an error? What is the error message? Is it not working the way you expect? What do you expect? What is it doing instead?
You have this line
This determines the order of the data in your bubble-sort like algorithm.
Just exchange it with - if (name[j].compareTo(name[j + 1]) > 0)
and it sorts it by name ascending instead.
If you want a high code quality, use object orientation, that means don't use parallel arrays to store the data, instead create a class Person and store your data in a single array of this class (or even better, in a List instead of in array), using Getter/Setters or fields of this class. -
// keep all data together in a single object
-
class Person {
-
public String name;
-
public int age;
-
-
// this method is for nice printing
-
public String toString() {
-
return "name: " + name + " age: " + age;
-
}
-
}
-
-
// declare storage for many object instances
-
List personList = new ArrayList();
-
-
// setup demo data
-
String[] name = {"B-name", "A-name"};
-
int[] age = {42, 18};
-
-
// store data
-
for (int i = 0, length = name.length; i < length; i++) {
-
Person p = new Person();
-
p.name = name[i];
-
p.age = age[i];
-
personList.add(p);
-
}
-
And second, don't reinvent sorting algrithms but use Collections.sort() -
// sort by name
-
class NameComparator implements Comparator {
-
public int compare(Object p1, Object p2) {
-
return ((Person)p1).name.compareTo(((Person)p2).name);
-
}
-
}
-
Collections.sort(personList, new NameComparator());
-
-
// print results
-
System.out.println("sorted person array=" + personList);
-
So if you run these two code snippets, it will print out: -
sorted person array=[name: A-name age: 18, name: B-name age: 42]
-
If you need more fields like "mark", "addresss" etc, put them into your Person class. If you need to sort by them, write a Comparator for each of them, for example MarkComparator, AddressComparator etc., and sort them by using -
Collections.sort(personList, new MarkComparator());
-
Collections.sort(personList, new AddressComparator());
-
You can even use anonymous inner classes for that, for example you do not declare a AgeComparator, but sort directly this way: -
Collections.sort(personList, new Comparator() {
-
public int compare(Object p1, Object p2) {
-
int age1 = ((Person)p1).age;
-
int age2 = ((Person)p2).age;
-
return (age1 > age2 ? 1 : (age1 == age2 ? 0 : -1));
-
}
-
});
-
You can even improve more, for example by defining a constructor for your class Person that takes as arguments all the data beloging to one person (Age, Name, etc.). Or if you don't read in arrays with the name, age etc. in the first way and then later assign them to a person list in a separate for-loop, but assign the data directly to a person and then put him in the list. And so on.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Xiaozhu |
last post by:
say, if you had parallel arrays containing the sales person id, the month, and
the sales figures (for that person in that month), sorting on sales figure and
preserve the order of figures within...
|
by: justanotherguy63 |
last post by:
Hi,
I am designing an application where to preserve the hierachy and for
code substitability, I need to pass an array of derived class object in
place of an array of base class object. Since I...
|
by: Jenny |
last post by:
Hi,
Can I create an array of tags by assigning same name to these tags?
For example, I have two <p> tags with the same name t1. But
document.all.b.value=document.all.t.length
does not...
|
by: Bob Jenkins |
last post by:
I have this cute data structure that I just found a second opportunity
to use. I googled for references to it, but came up empty. I
probably just don't know its name.
The algorithm on this...
|
by: nalbayo |
last post by:
what's the difference between
HttpContext.Current.User.Identity.Name;
and
Context.User.Identity.Name;
thanks!
|
by: David |
last post by:
Is there an effecient way to move an array entry up or down in the array?
For example. Say you have an array of 5 entries. They are all strings by the
way.
myArray =...
|
by: Socko |
last post by:
I'm trying to fix an sub routine in an VB module that basically reads in a MS database and writes it to an Excel Spread sheet. It works just fine except that the data isn't sorted correctly. I have...
|
by: tc18a |
last post by:
I'm looking for a fast algorithm for sorting an array of integers with respect to a master list.
For example:
Master List: 7 3 9 1 32 6 5 4 99 100 201 13
Array that needs to be sorted array1:...
|
by: sensure |
last post by:
why java array starts with zero and why pascal array stars with one?
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |