Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it L'avatar di SPEED78
    Registrato dal
    Jan 2003
    Messaggi
    358

    problema ordinamento array

    salve ragazzi ho un grandissimo problema e spero qualkuno di voi mi possa dare una mano:

    io ho una cartella con dei file immagine numerati così 1.jpg,2.jpg etc e fin qua nulla di strano
    il problema viene quando devo leggere i file con il for each perchè il file 10.jpg me lo mette dopo il file 1.jpg come fare io ho fatto così per ordinarli ma mi da errore:

    codice:
         Dim di As New IO.DirectoryInfo(Server.MapPath("immaginefinale"))
            Dim diar1 As IO.FileInfo() = di.GetFiles("*.jpg")
            Array.Sort(diar1)
            For Each dra In diar1
                response.write (Path.GetFileNameWithoutExtension((Server.MapPath  ("immaginefinale/" & dra.Name.ToString))))
            next
    errore
    At
    codice:
    least one object must implement IComparable. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.ArgumentException: At least one object must implement IComparable.
    
    Source Error: 
    
    
    Line 15:         Dim di As New IO.DirectoryInfo(Server.MapPath("immaginefinale"))
    Line 16:         Dim diar1 As IO.FileInfo() = di.GetFiles("*.jpg")
    Line 17:         Array.Sort(diar1)
    Line 18:         'Dim max As Integer = diar1.GetLowerBound(0)
    Line 19:         'Dim g As String = diar1.GetValue(diar1.GetUpperBound(0)).ToString
    grazie a tutti anticipatamente

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    ho fatto questo esempio

    il primo pulsante crea 20 files nominati 1.txt, 2.txt, ..., 20.txt

    il secondo pulsante visualizza la lista di quei files ordinati per nome, cioè da 1.txt, 2.txt,..., 20.txt

    Siccome è un argomento che non conosco bene, e c# ancora meno, prego altri amici più esperti ad intervenire




    codice:
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        /*--------------------------------------------------------------
        scrivo 20 files in c:\tmp\tmp, da 1.txt a 20.txt
        ---------------------------------------------------------------*/
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 20; i++)
            {
                string s = i.ToString() + ".txt";
                string f = Path.Combine("c:\\tmp\\tmp", s);
                //libreria.ModuloWeb.PrintLn(f);
                StreamWriter sw = File.CreateText(f);
                sw.Close();
            }
        }
    
        
        /*------------------------------------------------------------
         * lista i files ordinandoli per nome (ordine numerico)
         * 
         * ----------------------------------------------------------*/
        protected void Button2_Click(object sender, EventArgs e)
        {
            string path = "c:\\tmp\\tmp";
            string[] fs = Directory.GetFiles(path);
            Array.Sort(fs, new CompareByName());
            StringBuilder sb = new StringBuilder();
            sb.Append("<Table>");
            foreach (string f in fs)
            {
                //libreria.ModuloWeb.PrintLn(f);
                sb.Append("<tr><td>" + f + "</td></tr>");
            }
            sb.Append("</table>");
            this.form1.Controls.Add(new LiteralControl(sb.ToString()));
        }
    
        
        
        /*----------------------------------------------------------------
         * classe ausiliaria per ordinare per nome di file (numerico, 10 > 2)
         * 
         * --------------------------------------------------------------*/
        public class CompareByName: IComparer<string>
        {
            public int Compare(string o1, string o2) 
            {
                if (o1 == null && o2 == null) return 0;
                if (o1 == null) return 1;
                if (o2 == null) return -1;
                int s1 = int.Parse(Path.GetFileNameWithoutExtension(o1));
                int s2 = int.Parse(Path.GetFileNameWithoutExtension(o2));
                if (s1 > s2)
                    return 1;
                else if (s1 < s2)
                    return -1;
                else return 0;
            }
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Pagina senza titolo</title>
        <link href="../../stili/Styles.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="crea i files" /><asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Leggi files" /></div>
        </form>
    </body>
    </html>
    Pietro

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    questo è un altro modo per ordinare il vettore. Fa uso di un vettore che contiene le chiavi di ordinamento

    codice:
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        /*--------------------------------------------------------------
        scrivo 20 files in c:\tmp\tmp, da 1.txt a 20.txt
        ---------------------------------------------------------------*/
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 20; i++)
            {
                string s = i.ToString() + ".txt";
                string f = Path.Combine("c:\\tmp\\tmp", s);
                //libreria.ModuloWeb.PrintLn(f);
                StreamWriter sw = File.CreateText(f);
                sw.Close();
            }
        }
    
        
        /*------------------------------------------------------------
         * lista i files ordinandoli per nome (ordine numerico)
         * 
         * ----------------------------------------------------------*/
        protected void Button2_Click(object sender, EventArgs e)
        {
            string path = "c:\\tmp\\tmp";
            //vettore dei files
            string[] fs = Directory.GetFiles(path);
            
            //vettore ausiliario per l'ordinamento
            int[] va = new int[fs.Length];
            for (int i = 0; i < va.Length; i++)
                va[i] = int.Parse(Path.GetFileNameWithoutExtension(fs[i]));
            Array.Sort(va, fs);
            
            StringBuilder sb = new StringBuilder();
            sb.Append("<Table>");
            foreach (string f in fs)
            {
                sb.Append("<tr><td>" + f + "</td></tr>");
            }
            sb.Append("</table>");
            this.form1.Controls.Add(new LiteralControl(sb.ToString()));
        }
    
        
        
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Pagina senza titolo</title>
        <link href="../../stili/Styles.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="crea i files" /><asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Leggi files" /></div>
        </form>
    </body>
    </html>
    Pietro

  4. #4
    Utente di HTML.it L'avatar di SPEED78
    Registrato dal
    Jan 2003
    Messaggi
    358
    grazie mille pietro non me la cavo troppo bene con il c# mi servirebbe un esempio in vbnet.
    mi manca da capire cosa devo mettere in array.sort(mioarray,qua ) per il resto ho capito tutto

  5. #5
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Originariamente inviato da SPEED78
    grazie mille pietro non me la cavo troppo bene con il c# mi servirebbe un esempio in vbnet.
    mi manca da capire cosa devo mettere in array.sort(mioarray,qua ) per il resto ho capito tutto
    io lavoro in basic, ma considero un dovere conoscere un poco il c, perciò ho fatto le due paginette che ho mandato, e le conserverò per uso futuro

    provo a spiegare il 2 file.

    1) recupero i files dalla directory (potrei usare anche un filtro)
    string[] fs = Directory.GetFiles(path);


    2) debbo ordinare il vettore fs in base al nome del file, perciò genero un vettore ausiliario che in pratica contiene: 1, 2, 3, 4, 5..., 20.

    3) adesso ordino il vettore dei files, utilizzando il vettore ausiliario per l'ordinamento

    Array.Sort(va, fs);

    adesso il vettore fs è ordinato

    per altro sono qui
    Pietro

  6. #6
    Utente di HTML.it L'avatar di SPEED78
    Registrato dal
    Jan 2003
    Messaggi
    358
    spiegazione perfetta tnx

  7. #7
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    ho usato questo traduttore
    http://www.carlosag.net/Tools/CodeTr...r/Default.aspx

    che mi ha fatto questo (non ho verificato )
    codice:
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim path As String = "c:\"& vbTab&"mp\"& vbTab&"mp"
            'vettore dei files
            Dim fs() As String = Directory.GetFiles(path)
            'vettore ausiliario per l'ordinamento
            Dim va() As Integer = New Integer((fs.Length) - 1) {}
            Dim i As Integer = 0
            Do While (i < va.Length)
                va(i) = Integer.Parse(Path.GetFileNameWithoutExtension(fs(i)))
                i = (i + 1)
            Loop
            Array.Sort(va, fs)
            Dim sb As StringBuilder = New StringBuilder
            sb.Append("<Table>")
            For Each f As String In fs
                sb.Append(("<tr><td>"  _
                                + (f + "</td></tr>")))
            Next
            sb.Append("</table>")
            Me.form1.Controls.Add(New LiteralControl(sb.ToString))
        End Sub
    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.