decrement a foreach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elementary Penguin

    decrement a foreach


    is there a way to decrement a foreach loop?

    for example

    string s = "cat";

    foreach(char c in s)
    //some how it goes backward here
    Console.WriteLi ne(c);

    and so the result would be

    t
    a
    c

    --
    Texeme


  • robkit

    #2
    RE: decrement a foreach

    You could optionally resort the string in the reverse order and then run your
    foreach loop as usual. eg.

    class ReversedCompare r:IComparer
    {
    public int Compare(object x,object y)
    {
    if((char)x < (char)y)
    return 1;
    else if((char)x > (char)y)
    return -1;
    else
    return 0;
    }
    }


    Array array = s.ToCharArray() ;
    Array.Sort(arra y,new ReversedCompare r());
    //s = new String((char[])array);

    foreach(char c in array)
    {
    Console.WriteLi ne(c);
    }


    "Elementary Penguin" wrote:
    [color=blue]
    >
    > is there a way to decrement a foreach loop?
    >
    > for example
    >
    > string s = "cat";
    >
    > foreach(char c in s)
    > //some how it goes backward here
    > Console.WriteLi ne(c);
    >
    > and so the result would be
    >
    > t
    > a
    > c
    >
    > --
    > Texeme
    > http://texeme.com
    >
    >[/color]

    Comment

    Working...