473,408 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Sorting a javabean

My question is how to sort a javabean on the basis of two fields.
I have to create a javabean and then sort it by some String type field and then by another String type field.When I sort it for the second time the sorted sequence of the first field should not change and I have to use the ArrayList for
it.I have used comparator here but when I sort it the sorted sequence of the first field changes.
Please help me .
Apr 30 '07 #1
3 2330
r035198x
13,262 8TB
My question is how to sort a javabean on the basis of two fields.
I have to create a javabean and then sort it by some String type field and then by another String type field.When I sort it for the second time the sorted sequence of the first field should not change and I have to use the ArrayList for
it.I have used comparator here but when I sort it the sorted sequence of the first field changes.
Please help me .
What do you mean by "the sorted sequence of the first field changes."?
Did you read this tip?
Apr 30 '07 #2
My question is how to sort a javabean on the basis of two fields.
I have to create a javabean and then sort it by some String type field and then by another String type field.When I sort it for the second time the sorted sequence of the first field should not change and I have to use the ArrayList for
it.I have used comparator here but when I sort it the sorted sequence of the first field changes.
Please help me .
Initially I want to create a javabean whose parameters are name and some type, both are strings .I have to sort the bean first using the type field and then by name. when Iam sorting it by name the sequence of type also changes,this should not happen.I want to use the logic of order by clause of sql as the following:
Company OrderNumber
ABC Shop 5678
Sega 3412
Ws 6798
Ws 2312

Company OrderNumber
ABC Shop 5678
Sega 3412
Ws 2312
Ws 6798

Here I want to use ArrayList.Is it advisable to use the ArrayList.


My code is:
Expand|Select|Wrap|Line Numbers
  1.  import java.util.*; 
  2. class test3
  3. {
  4.      private String Type=null;
  5.      private String Name=null;
  6.  
  7.      test3(String a,String n)
  8.      {
  9.          Type=a;
  10.          Name=n;
  11.      }
  12.  
  13.      public String getName()
  14.      {
  15.          return Name;
  16.  
  17.      }
  18.         public void setName(String n)
  19.         {
  20.             this.Name=n;
  21.         }
  22.  
  23.         public String Type()
  24.         {
  25.             return Alerttype;
  26.  
  27.         }
  28.         public void setType(String a)
  29.         {
  30.          this.Type=a;    
  31. }
  32.  
  33.         static Comparator sortByType = new Comparator(){
  34.             public int compare(Object ch1, Object ch2) {
  35.                 String Type1 = ((test3) ch1).getType();
  36.                 String Type2 = ((test3) ch2).getType();
  37.  
  38.                 if(! alertType1.equals( Type2))
  39.                 return alertType1.compareTo( Type2);
  40.                 else
  41.  
  42.                     return 0;
  43.                 }
  44.                 };
  45.  
  46.                 static Comparator sortByName = new Comparator(){
  47.                     public int compare(Object ch1, Object ch2) {
  48.  
  49.                         String name1 = ((test3)ch1).getName();
  50.                         String name2= ((test3)ch2).getName();
  51.  
  52.                         if(! name1.equals(name2))
  53.                         return name1.compareTo( name2);
  54.                         else
  55.  
  56.                      return 0;    
  57.                     }
  58.                         };
  59.                     public String toString(){
  60.                         return Type + ":"+Name;
  61.                         }
  62.                         }
  63.  
  64.                         public class Show2{
  65.                         public Show2() {
  66.                         super();
  67.                         }
  68.                         public static void main(String[] args) {
  69.                         ArrayList a = new ArrayList();
  70.  
  71.                         test3 t = new test3("Check","name1");
  72.                         a.add(t);
  73.                         t=null;
  74.  
  75.                         t = new test3("Create","name2");
  76.                         a.add(t);
  77.                         t=null;
  78.  
  79.                         t = new test3("Attempt","name3");
  80.                         a.add(t);
  81.                         t=null; 
  82.                         Object[] o = a.toArray();
  83.                         System.out.println("before sorting");
  84.                         int size1 = a.size();
  85.                         for(int i =0; i<a.size();i++)
  86.                         {
  87.                         System.out.println((test3)o[i]);
  88.  
  89.                         }
  90.                         Arrays.sort(o,test3.sortByType);
  91.                         System.out.println("After sorting by Type");
  92.                         int size = a.size();
  93.                         for(int i =0; i<a.size();i++)
  94.                         {
  95.                         System.out.println((test3)o[i]);
  96.  
  97.                         }
  98.                         System.out.println("after sorting by name");
  99.                         Arrays.sort(o,test3.sortByName);
  100.                         int size2 = a.size();
  101.                         for(int i =0; i<a.size();i++)
  102.                         {
  103.                         System.out.println((test3)o[i]);
  104.  
  105.                         }
  106.                         }
  107.                         }     
  108.  
This is the output:
before sorting
Check:name1
Create:name2
Attempt:name3
After sorting by Type
Attempt:name3
Check:name1
create:name2
after sorting by name
Check:name1
create:name2
Attempt:name3

Here After sorting by name order of type should not change.

Thanks & Regards,
Anitha Tolgiri
May 2 '07 #3
Could any one suggest me how to do it.
May 3 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alex | last post by:
Hi all- I am having a little trouble storing resultset data from a javabean into an array located in a JSP page. Any other approaches, advice or links to information would be appreciated. Thanks...
4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
1
oll3i
by: oll3i | last post by:
Cd u please give me the example of javabean with constrained values thank u
1
by: mattmao | last post by:
Generally speaking, JSP and servlet are equivalent. I found one strange question: I've created a JavaBean class called UserinfoBean.class and put it inside /WEB-INF/classes/uts/ait/cmao/ It...
2
by: judge82 | last post by:
I have two JSPs, one takes a request and the other displays the result and I am having problem with the javabean to connect the two here are my codes takes request <%@page...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
2
by: devgupta01 | last post by:
Can I use formbean in place of javabean in struts framework..? Can you please explain difference between JavaBean and FormBean? Thanks in advance....
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.