When using (String.Format ("{0: # - # - ##}", item.codigo)) on an item in a list, the order by value_id is lost MVC C #

0

I have the following view that is responsible for showing the code in this format 9-9-99. And from the controller I indicate that it is ordered by the code, which is stored in full:

<table id="listado_table " class="table table-striped table-bordered dt-responsive" cellspacing="0" width="100%">
<thead>
    <tr style="background-color:#ff6a00;color:#fff">
        <th>Codigo</th>
        <th>Nombre</th>
        <th>Acciones</th>
    </tr>
</thead>
 <tbody>
    <% foreach (var item in Model.listado)
       { %>
    <tr>
        <td>
           <div style="display:none"> <%: item.codigo%></div>
            <%:             


            (String.Format("{0:#-#-##}", item.codigo)) %> // aquí le colocó String.Format, y se pierde el orden

        </td>
        <td>
            <%:item.nombre %>
        </td>

        <td class="center-block">
            acciones
        </td>
    </tr>
    <% } %>
</tbody>
</table>

But when I put it to ítem.codigo (String.Format ("{0: # - # - ##}", item.codigo)), it causes the whole order to be lost item.codigo, but when not it has (String.Format ("{0: # - # - ##}", item.codigo)), it is ordered. I would like to know if there is any way to order it in that format 9-9-99, without losing the order by code

In that order you should show me when with that format (I put it inside the red rectangle)

But he shows it to me like this:

But when I remove the String.Format, I can verify that they are ordered by code.

The problem is that it messes up when I put String.Format

    
asked by Danilo 27.08.2016 в 17:45
source

1 answer

1

Now you understand what .Net is doing. Thank you for clarifying with the screenshots.

What is happening is that the figures are subtracted first, because the% sign - does not recognize it as a separator, but as a subtraction operator.

Then:

1111 = 1-1-11 = -11
1200 = 1-2-00 = -1
1218 = 1-2-18 = -19
1233 = 1-2-33 = -34
1301 = 1-3-01 = -3

Now just orders:

-1  = 1-2-00
-3  = 1-3-01
-11 = 1-1-11
-19 = 1-2-18
-34 = 1-2-33

What is not understood is why it orders in ascending order in one and in descending order in another.

It may also be that you are ordering only for the first number, or only for the last two.

In any case I can think of two possible solutions:

  • 1) Remove Format from clause Order By
  • 2) Request a duplicate of the Codigo column, to apply the Order By to one column and the Format to the duplicate column.
answered by 28.08.2016 / 01:37
source