Sort A List In .net

Joined: 11/28/2008

I had a thought about a site I'm working on and wondered if this can be done in ASP.NET (C# or VB). For this I can NOT use a database.

On page software.aspx I want to have a list of software packages. It will have a name and 2 or 3 paragraph description. THere will be 5 or 6 of these softwares. Think of a table with six rows of text.

What I would like to do is have a link on another page something like www.site.com/software.aspx?x=2

When they click this link the software and description in row 2 will automatically be bumped to the top of the list. The order of the remaining rows isn't important.

Can something like this be done without a database?

Thanks,
Chris

Joined: 11/28/2008
how do you have your

how do you have your "softwares" stored in memory? I assume you have some sort of array/collection. 2 could be the index (perhaps subtract 1 for zero-based). In a session variable, you could keep track of the selected softwares: perhaps 2, and then 2, 5. First, display the selected softwares, and then as you loop through the full set of softwares, check if each is one of the selected softwares - if it is, don't display it again.

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
Using .net, I think you're

Using .net, I think you're going to want to use a hashtable. I would use this in conjunction with the method that paul described above.

Joined: 11/28/2008
Thanks guys...I was trying to

Thanks guys...I was trying to get all fancy with panels etc...I didn't even think of an array. I will work on that aspect once I get to that point (and study up on hash tables!)

I really appreciate it! /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" />

Chris

Joined: 11/28/2008
Hey guys...thought you'd like

Hey guys...thought you'd like to see my code behind file for how I got it work. The output is called from a function on the page. I'm definaely still a noobie with ASP.NET so I'm sure I have some very nasty code here that could stand a lot of refinement...but it works for now. This is not a finished version by any means...just a working basic version.

If you are on a page and hit the link software.aspx?sselect=2 it hits the software in the right order. iF you do it without the sselect variable it goes in default order:

CODE
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {        
        }
        public void SoftwareList()
                  {
        //code starts here

        //set the values for the array:
        string [] SoftwareList;
        SoftwareList = new string[7];
        string theoutput ="";
        int sselect;
        sselect = Convert.ToInt32(Request.QueryString["sselect"]);
        if (sselect < 1 || sselect > 6)
            {
            sselect=1;
            }
        //set each string of text
        SoftwareList[0] = "Software 0 - Win";
        SoftwareList[1] = "This is software 1 Java with a <a href=\"link\">link</a><br />";
        SoftwareList[2] = "Software 2 - Win<br />";
        SoftwareList[3] = "Software 2 - Mac<br />";
        SoftwareList[4] = "Software 3 - Win<br />";
        SoftwareList[5] = "Software 3 - Mac<br />";
        SoftwareList[6] = "Software 4 - Java<br />";

        //now show the list based on the selection
        for(int i = sselect;i<=6;i++)
            {
            theoutput=theoutput+SoftwareList[i];
            }
        if (sselect > 1)
            {
            for(int i = 1;i<=(sselect-1);i++)
                {
            theoutput=theoutput+SoftwareList[i];  
                }
            }
                Response.Write(theoutput);
                }
    }

Joined: 11/28/2008
Hi ChrisIs there an

Hi Chris

Is there an equivalent of PHP's "array_key_exists" function in C#? It would be easier to maintain if you could do something like:

CODE
        //set each string of text
        SoftwareList[0] = "Software 0 - Win";
        SoftwareList[1] = "This is software 1 Java with a <a href=\"link\">link</a><br />";
        SoftwareList[2] = "Software 2 - Win<br />";
        SoftwareList[3] = "Software 2 - Mac<br />";
        SoftwareList[4] = "Software 3 - Win<br />";
        SoftwareList[5] = "Software 3 - Mac<br />";
        SoftwareList[6] = "Software 4 - Java<br />";

        if (!array_key_exists(SoftwareList, sselect))
            {
            sselect=1;
            }

And also, instead of:

CODE
for(int i = sselect;i<=6;i++)

if you could do this:

CODE
for(int i = sselect;i<=count(SoftwareList);i++)

(I don't know C#) Basically, avoid hard-coding numbers where possible - let loops etc be defined by your data (at worst, define a constant at the top and use that throughout so you only have to change it in one place).

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
mmh,First thing off the bat,

mmh,

First thing off the bat, use int.parse instead on Convert.

try to keep the design/layout in the ASPX part and the code in ASPX.CS part.
Can I suggest using ASP:REPEATER control. Bind it to DataTable, DataTable can be made on the fly.

This way you have viewstate support.

If you don't want to use a database or a Datatable, you can use XML file and bind it to the Repeater control or even use XSL / XML this also good.

Many blog (like blogengine.net) uses XML files instead of a database.

Tip: Remember to put the XML files in the APP_DATA this allows you to write changes to the file, important to note on shared hosting