# -*- coding: utf-8 -*-
from ._version import __version__

import pytest

orders_map = {
    'first': 0,
    'second': 1,
    'third': 2,
    'fourth': 3,
    'fifth': 4,
    'sixth': 5,
    'seventh': 6,
    'eighth': 7,
    'last': -1,
    'second_to_last': -2,
    'third_to_last': -3,
    'fourth_to_last': -4,
    'fifth_to_last': -5,
    'sixth_to_last': -6,
    'seventh_to_last': -7,
    'eighth_to_last': -8,
}


def pytest_configure(config):
    """Register the "run" marker."""

    config_line = (
        'run: specify ordering information for when tests should run '
        'in relation to one another. Provided by pytest-ordering. '
        'See also: http://pytest-ordering.readthedocs.org/'
    )
    config.addinivalue_line('markers', config_line)


def pytest_collection_modifyitems(session, config, items):
    grouped_items = {}

    for item in items:

        for mark_name, order in orders_map.items():
            mark = item.get_marker(mark_name)

            if mark:
                item.add_marker(pytest.mark.run(order=order))
                break

        mark = item.get_marker('run')

        if mark:
            order = mark.kwargs.get('order')
        else:
            order = None

        grouped_items.setdefault(order, []).append(item)

    if grouped_items:
        unordered_items = grouped_items.pop(None, None)

        sorted_items = []
        prev_key = 0

        for key, ordered_items in grouped_items.items():

            if unordered_items and prev_key >= 0 and key < 0:

                sorted_items.extend(unordered_items)
                unordered_items = None

            prev_key = key

            sorted_items.extend(ordered_items)

        if unordered_items:
            sorted_items.extend(unordered_items)

        items[:] = sorted_items

@@ -1287,32 +1287,59 @@
  grouped_items:%0A
+        sorted_items = %5B%5D%0A%0A
         unordere
@@ -1348,16 +1348,17 @@
 items = 
+%5B
 grouped_
@@ -1377,13 +1377,12 @@
 ne, 
-None)
+%5B%5D)%5D
 %0A%0A  
@@ -1392,298 +1392,255 @@
    s
-orted_items = %5B%5D%0A        prev_key = 0%0A%0A        for key, ordered_items in grouped_items.items():%0A%0A            if unordered_items and prev_key %3E= 0 and key %3C 0:%0A%0A                sorted_items.extend(unordered_items)%0A                unordered_items = None%0A%0A            prev_key = key%0A%0A 
+tart_list = sorted((i for i in grouped_items.items() if i%5B0%5D %3E= 0), key=lambda x: x%5B0%5D)%0A        end_list = sorted((i for i in grouped_items.items() if i%5B0%5D %3C 0), key=lambda x: x%5B0%5D)%0A%0A        sorted_items.extend(%5Bi%5B1%5D for i in start_list%5D)%0A
         
    s
@@ -1627,35 +1627,32 @@
 _list%5D)%0A        
-   
 sorted_items.ext
@@ -1659,35 +1659,8 @@
 end(
-ordered_items)%0A%0A        if 
 unor
@@ -1666,30 +1666,26 @@
 rdered_items
-:%0A    
+)%0A
         sort
@@ -1704,54 +1704,105 @@
 end(
-unordered_items)%0A%0A        items%5B:%5D = sorted_items
+%5Bi%5B1%5D for i in end_list%5D)%0A%0A        items%5B:%5D = %5Bitem for sublist in sorted_items for item in sublist%5D
 %0A

Update the ordering function to sort based on the order considering non-contiguous blocks