首先. 我是在数据库中查询的几行数据.      比如 >
   id    name     sex     age 
      1     张三   男   18
   2  李四    男   17
   3  王五    男   12
   4  啊工    男   20
    查询出来之后, 保存在集合类中, 然后 根据 它们的年龄 进行排序. 打印出来.       强人麻烦 贴出代码. 就从保存在集合类中开始..  

解决方案 »

  1.   

    select ... sort by age;
      

  2.   

    override the comparable interface!
    重写比较的接口,呵呵
      

  3.   

    为何楼主不在从数据库取出来的时候就order by排序下.
      

  4.   

    建立一个person对象,并实现Comparable接口,在compareTo方法中对比person对象的年龄,之后在排序中就可以使用person1>person2等等来给对象排序了
      

  5.   

    Collections里的方法
    sort
    public static <T> void sort(List<T> list,
                                Comparator<? super T> c)
    现在所要做的事就是简单地写个实现类就行了
      

  6.   

    同情一把,不过java里面的排序很方便,应该掌握Collections.sort(list,
      new Comparator(){
        public int compareTo(Object p1,Object p2){return ((Student)p1).getAge()-((Student)p2).getAge();}
      }
    );
      

  7.   

    笔误:public int compare(Object p1,Object p2){return ((Student)p1).getAge()-((Student)p2).getAge();} 
      

  8.   


    package sort;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.*;
    import sun.text.CompactShortArray.Iterator;public class SortTest {
    public static void main(String[] args){
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Connection conn=DriverManager.getConnection("jdbc:odbc:xuwang");

    String sql="select * from student";

    Statement state=conn.createStatement();

    ResultSet rs=state.executeQuery(sql);

    ArrayList rl=new ArrayList();

                  //取出其数据 保存在 ArrayList中.
    while(rs.next()){
    rl.add(rs.getInt(1));    // 保存 ID
    rl.add(rs.getString(2));   //保存 name
    rl.add(rs.getString(3));     //保存 sex
    rl.add(rs.getInt(4));       //保存 age }

    java.util.Iterator it=rl.iterator();

    //使用 迭代进行打印.
                               //这里我打印了全部.. 就是不知道怎么把 年龄取出来进行排序. 然后打印出来. 包括其 姓名,年龄,性别.
    while(it.hasNext()){ 
    System.out.print(it.next());
    }
    System.out.println();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
      

  9.   


    估计考试是主要考你会不会对IComparable接口的运用
      

  10.   


    估计考试是主要考你会不会对IComparable接口的运用
      

  11.   

    package Exception3;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    public class Firstwork {
    //加载驱动
    public Statement state() {
    Connection conn = null;
    Statement stathe=null;
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:example", "sa", "fb");
    stathe= conn.createStatement();;
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return stathe;
    }
    //执行SQL语句
    public ResultSet rs() {
    ResultSet rs = null;
    try {
    Statement state = this.state();
    String sql = "select Id,Name,Age,Sex from student";
    rs = state.executeQuery(sql);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return rs;
    }
    //ArrayList中装对象.
    public ArrayList Secondwork() {
    ResultSet rs = this.rs();
    FileOutputStream output = null;
    ArrayList alist = new ArrayList();
    try {
    output = new FileOutputStream("e:/1.txt");
    while (rs.next()) {
    Stuent student = new Stuent();
    student.setId(rs.getInt(1));
    student.setName(rs.getString(2));
    student.setAge(rs.getInt(3));
    student.setSex(rs.getString(4));
    String str = "\r\n";
    byte[] wok = str.getBytes();

    int sid=student.getId()
    ;  //获取ID为int
    String ss=String.valueOf(sid);//将int转换为String
    byte[]  sidbyte=ss.getBytes(); //将String转换为byte

    int sage=student.getId();  //获取ID为int
    String s=String.valueOf(sage);//将int转换为String
    byte[]  sagebyte=ss.getBytes(); //将String转换为byte

    output.write(sidbyte);
    output.write(student.getName().getBytes());
    output.write(sagebyte);
    output.write(student.getSex().getBytes());
    output.write(wok);
    alist.add(student); 
    }
    output.close();
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return alist;
    }
     //打印结果
    public void print() {
    ArrayList a = this.Secondwork();
    for (int i = 0; i < a.size(); i++) {
    System.out.print(((Stuent) a.get(i)).getId() + " ");
    System.out.print(((Stuent) a.get(i)).getName() + " ");
    System.out.print(((Stuent) a.get(i)).getAge() + " ");
    System.out.print(((Stuent) a.get(i)).getSex() + "\n");

    }
    }
    }
      

  12.   

    package Exception3;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    public class Firstwork {
    //加载驱动
    public Statement state() {
    Connection conn = null;
    Statement stathe=null;
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:example", "sa", "fb");
    stathe= conn.createStatement();;
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return stathe;
    }
    //执行SQL语句
    public ResultSet rs() {
    ResultSet rs = null;
    try {
    Statement state = this.state();
    String sql = "select Id,Name,Age,Sex from student";
    rs = state.executeQuery(sql);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return rs;
    }
    //ArrayList中装对象.
    public ArrayList Secondwork() {
    ResultSet rs = this.rs();
    FileOutputStream output = null;
    ArrayList alist = new ArrayList();
    try {
    output = new FileOutputStream("e:/1.txt");
    while (rs.next()) {
    Stuent student = new Stuent();
    student.setId(rs.getInt(1));
    student.setName(rs.getString(2));
    student.setAge(rs.getInt(3));
    student.setSex(rs.getString(4));
    String str = "\r\n";
    byte[] wok = str.getBytes();

    int sid=student.getId()
    ;  //获取ID为int
    String ss=String.valueOf(sid);//将int转换为String
    byte[]  sidbyte=ss.getBytes(); //将String转换为byte

    int sage=student.getId();  //获取ID为int
    String s=String.valueOf(sage);//将int转换为String
    byte[]  sagebyte=ss.getBytes(); //将String转换为byte

    output.write(sidbyte);
    output.write(student.getName().getBytes());
    output.write(sagebyte);
    output.write(student.getSex().getBytes());
    output.write(wok);
    alist.add(student); 
    }
    output.close();
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return alist;
    }
     //打印结果
    public void print() {
    ArrayList a = this.Secondwork();
    for (int i = 0; i < a.size(); i++) {
    System.out.print(((Stuent) a.get(i)).getId() + " ");
    System.out.print(((Stuent) a.get(i)).getName() + " ");
    System.out.print(((Stuent) a.get(i)).getAge() + " ");
    System.out.print(((Stuent) a.get(i)).getSex() + "\n");

    }
    }
    }排序就没写了..
      

  13.   

    有好几种方法来排序
     讲一下原理:
      1 通过SQL语句 select * from table order by Column
      2 如果没对SQL语句用order by 
        哪就把结果集放入Vector 中,
        通过Vector 的排序方法进行排序,
        也可以自己写算法进行排序
      

  14.   

    这明显就是正解阿!!!!
    fosjos,谢谢了