yohpopsMedlem sedan feb. 20011 198 inlägg Hej.
Jag har en fillista som jag vill sortera på delar av namn.
Filerna har syntaxen fakturanummer_kundnummer_datum.PDF
Hur gör jag för att sortera på t.ex kundnummer.
String filearr[] = new String[4];
String invno = request.getParameter("invno");
String custno = request.getParameter("custno");
String dat = request.getParameter("dat");
String bgcolor ="";
boolean found = false ;
boolean binvno = true ;
boolean bcustno = true ;
boolean bdat = true ;
fl = new FileList(dir, order);
File f;
String fileLine = null ;
for (Iterator i = fl.iterator();i.hasNext();)
{
// create one line
f = (File) i.next();
filearr[0] = f.getName();
if (!filearr[0].substring(filearr[0].indexOf(".",0),filearr[0].length()).equals(".TXT") && !filearr[0].substring(filearr[0].indexOf(".",0),filearr[0].length()).equals(".txt"))
{
int first = filearr[0].indexOf("_",0);
int next = filearr[0].indexOf("_",first+1);
int last = filearr[0].indexOf("_",next+1);
filearr[1] = filearr[0].substring(0,first);
filearr[2] = filearr[0].substring(first+1,next);
filearr[3] = filearr[0].substring(next+1,last);
if (!invno.equals(""))
{
if (invno.equals(filearr[1]))
{
binvno=true;
}
else
{
binvno=false;
}
}
if (!custno.equals(""))
{
if (custno.equals(filearr[2]))
{
bcustno=true;
}
else
{
bcustno=false;
}
}
if (!dat.equals(""))
{
if (dat.equals(filearr[3]))
{
bdat=true;
}
else
{
bdat=false;
}
}
if (binvno && bcustno && bdat)
{
if (bgcolor=="")
{
bgcolor="#DDDDDD";
}
else
{
bgcolor="";
}
fileLine = "<tr bgcolor='"+ bgcolor +"'><td onclick=\"openFile('"+ filearr[0] +"')\" style=\"cursor:hand;\">" + filearr[1] + "</td>" ;
fileLine += "<td onclick=\"openFile('"+ filearr[0] +"')\" style=\"cursor:hand;\">" + filearr[2] + "</td>" ;
fileLine += "<td onclick=\"openFile('"+ filearr[0] +"')\" style=\"cursor:hand;\">" + f.length() + "</td>" ;
fileLine += "<td onclick=\"openFile('"+ filearr[0] +"')\" style=\"cursor:hand;\">" + filearr[3] + "</td>" ;
fileLine += "</tr>" ;
out.println(fileLine);
found = true ;
}
}
}
Nån som vet?
mvh
Johan
LimeMedlem sedan sep. 2001961 inlägg Ja, lagra filnamnen i något som går att sortera, d.v.s. valfri som ärver av Collection. Går att skapa en sådan från en array. Se till att de är Comparable och använd sedan sort-metoden på Collection.
/Lime
yohpopsMedlem sedan feb. 20011 198 inlägg beskriv
Kan du beskriva lite mer.
Jag har ganska ny på jsp.
Har bara modifierat befintlig kod för att få detta att fungera.
mvh
Johan
LimeMedlem sedan sep. 2001961 inlägg Okej... Jag har skrivit 3 klasser.
CustomerFile.java är den klass som håller reda på kundfilsdata. Skippa fibblandet med arrayer. Denna är inte helt klar... Borde skapa get/set-metoder och göra attributen privata.
CustomerFileComparator.java är Comparator-klassen. Har bara implementerat defaultfallet.
CFTest.java är en lite testklass för att visa vad som händer.
CustomerFile.java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
/*
* Created on 2004-mar-05
*
*/
/**
* @author lime
*/
public class CustomerFile {
// Borde vara private och med accessorer
public int customerNumber;
public Date invoiceDate;
public int invoiceNumber;
public CustomerFile()
{
}
public CustomerFile(int customerNumber, Date invoiceDate, int invoiceNumber)
{
this.customerNumber = customerNumber;
this.invoiceDate = invoiceDate;
this.invoiceNumber = invoiceNumber;
}
public static CustomerFile parse(String fileName)
{
String strCustNum, strInvDate, strInvNum;
int cNum, invNum;
Date invDate;
try {
StringTokenizer st = new StringTokenizer(fileName, "_");
if(st.countTokens() == 3)
{
strInvNum = (String) st.nextElement();
strCustNum = (String) st.nextElement();
strInvDate = (String) st.nextElement();
// Skala bort .<postfix> från strInvDate
strInvDate = strInvDate.substring(0, strInvDate.indexOf('.'));
invNum = Integer.parseInt(strInvNum);
cNum = Integer.parseInt(strCustNum);
// Jag har antagit att datumet är i formatet YYYYMMdd
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
invDate = sdf.parse(strInvDate);
return new CustomerFile(cNum,invDate,invNum);
}
else
{
// Det var inte på formatet
// fakturanummer_kundnummer_datum.PDF
// allså gör vi något annat...
}
} catch (Exception e) {
// TODO: handle exception
}
return new CustomerFile();
}
public String toString()
{
return "cNum = " + customerNumber + " iNum = " + invoiceNumber + " iDate = " + invoiceDate;
}
}
CustomerFileComparator.java
import java.util.Comparator;
/*
* Created on 2004-mar-05
*
*/
/**
* @author lime
*
*/
public class CustomerFileComparator implements Comparator {
private int sortOn = CUSTOMER_NUMBER;
public static final int CUSTOMER_NUMBER = 1;
public static final int INVOICE_NUMBER = 2;
public static final int INVOICE_DATE = 3;
public CustomerFileComparator()
{
}
public CustomerFileComparator(int sortOn){
this.sortOn = sortOn;
}
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object o1, Object o2) {
int result = 0;
CustomerFile cf1 = null;
CustomerFile cf2 = null;
try {
if(o1 instanceof CustomerFile)
cf1 = (CustomerFile) o1;
if(o2 instanceof CustomerFile)
cf2 = (CustomerFile) o2;
// Om båda objekten är CustomerFiles så jämför dom med varandra.
if(cf1 != null && cf2 != null)
{
switch (sortOn) {
case CUSTOMER_NUMBER :
result = cf1.customerNumber - cf2.customerNumber;
break;
default :
result = 0;
break;
}
}
} catch (Exception e) {
// TODO: handle exception
result = 0;
}
return result;
}
}
CFTest.java
import java.util.*;
/*
* Created on 2004-mar-05
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author lime
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class CFTester {
public static void main(String[] args) {
Vector customerFiles = new Vector();
// Lägger till en bunt "filer" i formatet fakturanummer_kundnummer_datum.PDF
customerFiles.add(CustomerFile.parse("1_14_20040313.pdf"));
customerFiles.add(CustomerFile.parse("2_13_20040313.pdf"));
customerFiles.add(CustomerFile.parse("3_12_20040313.pdf"));
customerFiles.add(CustomerFile.parse("4_11_20040313.pdf"));
customerFiles.add(CustomerFile.parse("5_10_20040313.pdf"));
customerFiles.add(CustomerFile.parse("6_9_20040313.pdf"));
customerFiles.add(CustomerFile.parse("7_111_20040313.pdf"));
Collections.sort(customerFiles, new CustomerFileComparator());
for (Iterator iter = customerFiles.iterator(); iter.hasNext();) {
CustomerFile element = (CustomerFile) iter.next();
System.out.println(element.toString());
}
}
}
yohpopsMedlem sedan feb. 20011 198 inlägg Tack
men jag lyckades lösa det på ett annat enklare sätt
mvh
Johan
SirPeterMedlem sedan feb. 200115 571 inlägg Skriv gärna hur du löste problemet istället för att markera ett inlägg som inte ger någon info alls om hur någon kan lösa ett liknande problem framöver.
yohpopsMedlem sedan feb. 20011 198 inlägg Lösning
Såhär blev koden.
String filearr[][] = new String[300][5];
FileList fl ;
fl = new FileList(dir, order);
File f;
int o = 0;
long ltemp = 0;
for (Iterator i = fl.iterator();i.hasNext();)
{
// create one line
f = (File) i.next();
filearr[o][0] = f.getName();
ltemp = f.length();
if (!filearr[o][0].substring(filearr[o][0].indexOf(".",0),filearr[o][0].length()).equals(".TXT") && !filearr[o][0].substring(filearr[o][0].indexOf(".",0),filearr[o][0].length()).equals(".txt"))
{
int first = filearr[o][0].indexOf("_",0);
int next = filearr[o][0].indexOf("_",first+1);
int last = filearr[o][0].indexOf("_",next+1);
filearr[o][1] = filearr[o][0].substring(0,first);
filearr[o][2] = filearr[o][0].substring(first+1,next);
filearr[o][3] = filearr[o][0].substring(next+1,last);
filearr[o][4] = ltemp + "";
if (!invno.equals(""))
{
if (invno.equals(filearr[o][1]))
{
binvno=true;
}
else
{
binvno=false;
}
}
if (!custno.equals(""))
{
if (custno.equals(filearr[o][2]))
{
bcustno=true;
}
else
{
bcustno=false;
}
}
if (!dat.equals(""))
{
if (dat.equals(filearr[o][3]))
{
bdat=true;
}
else
{
bdat=false;
}
}
if (binvno && bcustno && bdat)
{
o++;
found = true;
}
}
}
if (found)
{
int reallen = 0;
for (int z=0;z<filearr.length;z++)
{
if (filearr[z][0].equals(null) || filearr[z][0].substring(filearr[z][0].indexOf(".",0),filearr[z][0].length()).equals(".TXT") || filearr[z][0].substring(filearr[z][0].indexOf(".",0),filearr[z][0].length()).equals(".txt"))
{
reallen = z;
break;
}
}
String temp[] = new String[1];
for (int z=0;z<reallen;z++)
{
for (o=0;o<reallen;o++)
{
if (Integer.parseInt(filearr[o][sort]) > Integer.parseInt(filearr[z][sort]))
{
temp = filearr[o];
filearr[o] = filearr[z];
filearr[z] = temp;
}
}
}
for (o=0;o<reallen;o++)
{
if (bgcolor=="")
{
bgcolor="#DDDDDD";
}
else
{
bgcolor="";
}
fileLine = "<tr bgcolor='"+ bgcolor +"'><td onclick=\"openFile('"+ filearr[o][0] +"')\" style=\"cursor:hand;\">" + filearr[o][1] + "</td>" ;
fileLine += "<td onclick=\"openFile('"+ filearr[o][0] +"')\" style=\"cursor:hand;\">" + filearr[o][2] + "</td>" ;
int l_size = Integer.parseInt(filearr[o][4]);
if (l_size>1000)
{
fileLine += "<td onclick=\"openFile('"+ filearr[o][0] +"')\" style=\"cursor:hand;\">" + (l_size/1000) + " kb</td>" ;
}
else
{
fileLine += "<td onclick=\"openFile('"+ filearr[o][0] +"')\" style=\"cursor:hand;\">" + l_size + " b</td>" ;
}
fileLine += "<td onclick=\"openFile('"+ filearr[o][0] +"')\" style=\"cursor:hand;\">" + filearr[o][3] + "</td>" ;
fileLine += "</tr>" ;
out.println(fileLine);
}
}
Förmodligen inte den bästa men det ska bara fungera ett tag.
Vi ska byta system inom kort.