1

I have data IList<String> type in my Model returning string date in yyyyMM format, I need help to display array elements in radio button sorted descending order in MM/yyyy format.

My Model look like as follows:

 public class DataToLoad
 {
        public List<string> Months{ get; set; }       
 }

In my view I have the following to display array element in radio button.

foreach (var record in Model.Months)
{
  <div class="radio col-12">
     <div class="row">
       <div class="col-5" style="">
         @Html.RadioButton("Months", record, true, new { id = record, @class = "m-r" })<label>@record</label>
        </div>
      </div>
   </div>
}

I need if Months return 199008 and 19909, I want to sort values descending and format as MM/yyyy and display the values using radio button 09/1990 first then 08/1990. Any Help?

Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
kuka muk
  • 349
  • 2
  • 18

2 Answers2

2

You can use this code:

foreach (var record in Model.Months.Select(x=>$"{x.Substring(4,2)}/{x.Substring(0,4)}").OrderByDescending(x=>x))
{
     <div class="radio col-12">
        <div class="row">
          <div class="col-5" style="">
            @Html.RadioButton("Months", record, true, new { id = record, @class = "m-r" })<label>@record</label>
           </div>
         </div>
      </div>
}
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
  • 1
    Thank you that works Perfect!, Is there any way to select the oldest date by default? for example if dates are `199008 and 19909` by default 08/1990 radio button auto selected?. Right now when order desc selects the old date as a default order ascending selects the recent date – kuka muk Jan 05 '21 at 17:57
  • @kukamuk you are welcome. Of course after order by descending, the last item on the list is the oldest date. If you could use for instead of foreach, if you checked = "true" for the case where i == Model.Months.Count()-1, the default will be selected. For more information about checked https://stackoverflow.com/a/18010222/8810311 – Ramil Aliyev 007 Jan 05 '21 at 18:11
  • Thanks Ramil!. `where i == Model.Months.Count()-1` using `foreach`? – kuka muk Jan 05 '21 at 18:13
1

Same as any other piece of C# code, use Linq:

   @foreach (var record in Model.Months.OrderBy(x => x))
   {
     <div class="radio col-12">
        <div class="row">
          <div class="col-5" style="">
            @Html.RadioButton("Months", record, true, new { id = record, @class = "m-r" })<label>@record</label>
           </div>
         </div>
      </div>
    }
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Thank you!! yes that work but how can i format string as `MM/yyyy` format and auto select the first one currently display as yyyyMM? – kuka muk Jan 05 '21 at 17:46
  • In the label I assume? Again, same as any other C# date formatting code: `` – Blindy Jan 05 '21 at 17:48
  • Oh I just noticed they're actual strings, in that case you want to make it a `DateTime` first (you should actually only use `DateTime`, never strings): – Blindy Jan 05 '21 at 17:52