java - sort line by last name and in order -


ok have code far sorts job position, needs sort last name , in alphabetical order. ive managed make sort position give average salary , total.

import java.io.bufferedreader; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.util.arraylist; import java.util.list;  public class faculty {      private static final string assistant = "assistant";     private static final string associate = "associate";     private static final string full = "full";      public static void main(string[] args) {         fileinputstream filestream;         bufferedreader reader;         printwriter writer;         string line;          double totalassistant = 0;         double totalassociate = 0;         double totalfull = 0;          try {             filestream = new fileinputstream("faculty list.txt");             reader = new bufferedreader(new inputstreamreader(filestream));             writer = new printwriter("test.txt");              list<string> assistantlist = new arraylist<string>();             list<string> associatelist = new arraylist<string>();             list<string> fulllist = new arraylist<string>();              while ((line = reader.readline()) != null) {                  string[] split = line.split(" ");                  double value = double.parsedouble(split[split.length - 1]);                  string type = split[split.length - 2];                  if (assistant.equals(type)) {                     assistantlist.add(line);                  } else if (associate.equals(type)) {                     totalassociate += value;                     associatelist.add(line);                  } else if (full.equals(type)) {                     totalfull += value;                     fulllist.add(line);                 }             }              writeinfileoutput(writer, totalassistant, assistantlist);             writeinfileoutput(writer, totalassociate, associatelist);             writeinfileoutput(writer, totalfull, fulllist);              reader.close();              writer.close();         } catch (ioexception e) {             system.out.println(e);          } {             reader = null;             filestream = null;             writer = null;         }      }      private static void writeinfileoutput(printwriter writer, double totalsalary, list<string> listlines) {         (string assistant : listlines) {             writer.append(assistant).append("\n");         }          writer.append("-------\n");         writer.append("total salary:$").append(string.valueof(totalsalary)).append("\n");         writer.append("average salary: $")                         .append(string.valueof(totalsalary / listlines.size())).append(" \n\n");     }  } 

output

pablo bailey educ associate 68757.00 lonnie williamson gens associate 134777.00 raymond page educ associate 120150.00 wallace fitzgerald busn associate 40889.00 juana robbins sobl associate 93669.00 steven hall sobl associate 117532.00 melissa davis educ associate 132186.00 karla valdez busn associate 16385.00 melba luna hlth associate 70358.00 sonja washington hlth associate 59302.00 julio diaz hlth associate 102641.00 virgil briggs pac associate 40936.00 terrell sherman educ associate 161595.00 jorge scott csis associate 124175.00 tanya duncan busn associate 178894.00 troy cannon busn associate 58890.00  -------   total salary: $3645049.0  average salary: $104144.25714285714  

that's current output is, sorted rank. have add list sort last name first followed first name alphabetically.this small sample of output cause text file pretty long. new lines can added output.

i make stuff more object oriented. means: introduce new object employee has fields following example:

public class employee {     private string firstname;     private string familyname;     private string department;     private enum jobposition;     private double salary;      // put constructor , getters , setters field here }  public enum jobposition {     assistant,     associate,     full } 

when reading input, split fields need create employee , create 1 list employees. because have objects instead of string containing multiple properties of 1 employee can sort objects on each property want.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -